What I want to achieve:
Have a <Map><FeatureGroup><Circle />[1 or more]...</FeatureGroup></Map>
hierarchy and fit the map bounds to the feature group so that all the circles are in the viewport.
If there is only one circle, it should fit the bounds (ie: zoom in on) to that circle.
What I've tried:
- giving
FeatureGroup
aref
and callinggetBounds
on it to pass ontoMap
. Because of the lifecycleFeatureGroup
doesn't exist at the timecomponentDidMount
is called - it gets rendered later (https://github.com/PaulLeCam/react-leaflet/issues/106#issuecomment-161594328). - Storing
Circle
in state and calling getBounds on that (assuming, in this case, that there is only one circle. That didn't work either.
I think I might need to do something with the React Context but I'm not sure that I fully understand it right now, so I need some help.
Other information
I'm using react-leaflet@2.1.2
Thanks for any help offered!
Have you tried doing
getBounds
in thecomponentDidMount
function instead ofcomponentWillMount
? If that doesn't work then I'd suggest extending theFeatureGroup
component and adding anonLoaded
function as as prop and call that function in thecomponentDidMount
function of your extended component. And by extending theFeatureGroup
component I actually mean copying/pasting it from here. (if you care about why you need to copy that whole file check this thread)This isn't tested but your code will probably look something like
Note: If you are using
react-leaflet
V1 this is actually way easier and I can edit this answer with that code if needed.Because the contents of the
Map
are unavailable atcomponentDidMount
-time (https://github.com/PaulLeCam/react-leaflet/issues/106#issuecomment-161594328) you cannot get the bounds of theFeatureGroup
at that point, and out of all therefs
you assign, only theMap
ref will be available inthis.refs
.However, as per this GitHub comment: https://github.com/PaulLeCam/react-leaflet/issues/106#issuecomment-366263225 you can give a
FeatureGroup
anonAdd
handler function:and you can then use the
Map
refs to access theleafletElement
and callfitBounds
with the bounds of the incoming event target, which will be theFeatureGroup
:This will then "zoom" the map into the bounds of your
FeatureGroup
, as desired.Update
I modified my React component so that zoom and centre are controlled by query parameters. The problem with the above solution was that if you zoomed in on a
MarkerClusterGroup
by clicking on it, for example, it would update the zoom in the url, re-render the map and re-callonFeatureGroupAdd
, thus undoing all the marker cluster goodness.What I needed was to access the zoom level required to keep the newly drawn circle nicely in bounds, then update the url with the correct zoom level and center.
In order to be able to control the whole map I also call
functionToUpdateUrl
inonZoomEnd
andonDragEnd
event handlers, like so:and one for handling cluster clicks:
Then, when rendering the Map element, pass these properties:
And remember to give any
MarkerClusterGroup
s theironClusterClick
callback: