folium icon indicating copy to clipboard operation
folium copied to clipboard

Possible to change bounds of map based on what is selected by LayerControl

Open galewis2 opened this issue 5 years ago • 4 comments

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!

galewis2 avatar Apr 27 '20 19:04 galewis2

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 FeatureGroup class.
  • 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_bounds method we could use.
  • The map has a method to fit to bounds. Haven't tried this approach though.

Conengmo avatar Apr 29 '20 06:04 Conengmo

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

galewis2 avatar Apr 29 '20 15:04 galewis2

@Conengmo I assume this would be best placed in a plugin as it's not core Leaflet functionality?

galewis2 avatar Apr 29 '20 19:04 galewis2

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.

Conengmo avatar Apr 30 '20 06:04 Conengmo