Possible to change bounds of map based on what is selected by LayerControl
In short, is it possible to have Folium generate a map whereby the map is zoomed and/or bounding box changed when the user changes the layer?
I'm currently using FeatureGroups added to the map, overlay off (so one layer can be viewed at a time). I would like to automatically have the map zoom to the featuregroup extent when selected.
Thanks!
Hi @galewis2 , I don't think this is currently implemented in folium. It sounds like a nice feature. If someone wants to whip up a PR for this I'd gladly review it.
Some thoughts:
- For starters we could apply this to the
FeatureGroupclass. - I suppose this doesn't apply to tile layers because they cover the whole map.
- Does this apply to other classes as well? I'd rather not make it too complicated.
- The Leaflet FeatureGroup class has a
get_boundsmethod we could use. - The map has a method to fit to bounds. Haven't tried this approach though.
Duly noted, I had a look at the code and it's not too bad. I have some experience with Jinja2 and way back used Leaflet quite a bit.
As a first attempt, I'm considering doing this as a boolean on LayerControl and drop in a map.on action when BaseLayer change as a first step. Unfortunately that only covers overlays, may need a bit more work to cover featuregroups.
That way if a FeatureGroup is specifically excluded from LayerControl, it won't have those properties as they're irrelevant to zoom to function
@Conengmo I assume this would be best placed in a plugin as it's not core Leaflet functionality?
You don't have to move it to a plugin, because it uses only Leaflet native stuff. Plugins are for things that use other external resources, such as Leaflet plugins.
Let's take the rest of the discussion to the PR.