Skip to content

Zoom to Overlay Selection #1317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion folium/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class LayerControl(MacroElement):
autoZIndex : bool, default True
If true the control assigns zIndexes in increasing order to all of
its layers so that the order is preserved when switching them on/off.
zoom_selected : bool, default False
If true the map zooms to the selected layers.
**kwargs
Additional (possibly inherited) options. See
https://leafletjs.com/reference-1.6.0.html#control-layers
Expand Down Expand Up @@ -128,13 +130,31 @@ class LayerControl(MacroElement):
{%- for val in this.layers_untoggle.values() %}
{{ val }}.remove();
{%- endfor %}

{%- if this.zoom_selected is sameas true %}
{{this._parent.get_name()}}.on('overlayadd overlayremove', function(e) {
var bounds = L.latLngBounds([]);
var c = 0;
{{this._parent.get_name()}}.eachLayer(function(layer){
if (typeof layer.getBounds === 'function') {
bounds.extend(layer.getBounds());
c+=1;
}
});
if (c > 0) {
{{this._parent.get_name()}}.flyToBounds(bounds);
}
});
{%- endif %}

{% endmacro %}
""")

def __init__(self, position='topright', collapsed=True, autoZIndex=True,
def __init__(self, position='topright', collapsed=True, autoZIndex=True, zoom_selected=False,
**kwargs):
super(LayerControl, self).__init__()
self._name = 'LayerControl'
self.zoom_selected = zoom_selected
self.options = parse_options(
position=position,
collapsed=collapsed,
Expand Down