Skip to content

Commit

Permalink
Merge pull request #28416 from rakshitjain13/map-bbox
Browse files Browse the repository at this point in the history
added coordinates in get bounds
  • Loading branch information
chiragsalian authored Oct 2, 2023
2 parents 9cfea1a + ce09532 commit bf05f13
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/components/MapView/MapView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ const MapView = forwardRef<MapViewHandle, MapViewProps>(({accessToken, style, ma
centerCoordinate: waypoints[0].coordinate,
});
} else {
const {southWest, northEast} = utils.getBounds(waypoints.map((waypoint) => waypoint.coordinate));
const {southWest, northEast} = utils.getBounds(
waypoints.map((waypoint) => waypoint.coordinate),
directionCoordinates,
);
cameraRef.current?.fitBounds(northEast, southWest, mapPadding, 1000);
}
}
return () => {
setIsIdle(false);
};
}, [mapPadding, waypoints, isIdle]),
}, [mapPadding, waypoints, isIdle, directionCoordinates]),
);

useEffect(() => {
Expand Down
7 changes: 5 additions & 2 deletions src/components/MapView/MapView.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ const MapView = forwardRef<MapViewHandle, MapViewProps>(

const map = mapRef.getMap();

const {northEast, southWest} = utils.getBounds(waypoints.map((waypoint) => waypoint.coordinate));
const {northEast, southWest} = utils.getBounds(
waypoints.map((waypoint) => waypoint.coordinate),
directionCoordinates,
);
map.fitBounds([northEast, southWest], {padding: mapPadding});
}, [waypoints, mapRef, mapPadding]);
}, [waypoints, mapRef, mapPadding, directionCoordinates]);

useEffect(() => {
if (!mapRef) {
Expand Down
6 changes: 5 additions & 1 deletion src/components/MapView/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
function getBounds(waypoints: Array<[number, number]>): {southWest: [number, number]; northEast: [number, number]} {
function getBounds(waypoints: Array<[number, number]>, directionCoordinates: undefined | Array<[number, number]>): {southWest: [number, number]; northEast: [number, number]} {
const lngs = waypoints.map((waypoint) => waypoint[0]);
const lats = waypoints.map((waypoint) => waypoint[1]);
if (directionCoordinates) {
lngs.push(...directionCoordinates.map((coordinate) => coordinate[0]));
lats.push(...directionCoordinates.map((coordinate) => coordinate[1]));
}

return {
southWest: [Math.min(...lngs), Math.min(...lats)],
Expand Down

0 comments on commit bf05f13

Please sign in to comment.