Skip to content

Commit

Permalink
Add automatically fit the map to the circle's bounds in RangeMap (#190)
Browse files Browse the repository at this point in the history
* Add automatically fit the map to the circle's bounds in RangeMap

As the custom search range changes (e.g. 175m -> 200m), the map bounds would fit accordingly automatically.

* fix sometimes not fitting to the latest bounds
  • Loading branch information
desmondlsl authored Jun 23, 2024
1 parent db6e322 commit 9fdf4a5
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/components/home/RangeMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ const RangeMap = React.forwardRef<Leaflet.Map, RangeMapProps>(
};
}, [handleMove, map]);

useEffect(() => {
const bounds = circleRef.current?.getBounds();
bounds &&
map?.fitBounds(bounds, {
// Disable animation to prevent not fitting to the latest bounds when called multiple times quickly
// See: Glitchy behavior when animating fitBounds multiple times https://github.com/Leaflet/Leaflet/issues/3249
animate: false,
});
}, [map, range]);

return (
<MapContainer
style={{ height: "100%", position: "relative" }}
Expand Down

0 comments on commit 9fdf4a5

Please sign in to comment.