Skip to content

Commit 86319bb

Browse files
authored
Geojson zoom on click changes (#1349)
* Only zoom on click if method is available * Add zoom_on_click parameter * Update GeoJSON_and_choropleth.ipynb
1 parent 5b61d9a commit 86319bb

File tree

2 files changed

+105
-125
lines changed

2 files changed

+105
-125
lines changed

examples/GeoJSON_and_choropleth.ipynb

Lines changed: 95 additions & 123 deletions
Large diffs are not rendered by default.

folium/features.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ class GeoJson(Layer):
354354
embed: bool, default True
355355
Whether to embed the data in the html file or not. Note that disabling
356356
embedding is only supported if you provide a file link or URL.
357+
zoom_on_click: bool, default False
358+
Set to True to enable zooming in on a geometry when clicking on it.
357359
358360
Examples
359361
--------
@@ -409,9 +411,13 @@ class GeoJson(Layer):
409411
e.target.setStyle({{ this.get_name() }}_highlighter(e.target.feature));
410412
},
411413
{%- endif %}
414+
{%- if this.zoom_on_click %}
412415
click: function(e) {
413-
{{ this.parent_map.get_name() }}.fitBounds(e.target.getBounds());
416+
if (typeof e.target.getBounds === 'function') {
417+
{{ this.parent_map.get_name() }}.fitBounds(e.target.getBounds());
418+
}
414419
}
420+
{%- endif %}
415421
});
416422
};
417423
var {{ this.get_name() }} = L.geoJson(null, {
@@ -438,7 +444,8 @@ class GeoJson(Layer):
438444

439445
def __init__(self, data, style_function=None, highlight_function=None, # noqa
440446
name=None, overlay=True, control=True, show=True,
441-
smooth_factor=None, tooltip=None, embed=True, popup=None):
447+
smooth_factor=None, tooltip=None, embed=True, popup=None,
448+
zoom_on_click=False):
442449
super(GeoJson, self).__init__(name=name, overlay=overlay,
443450
control=control, show=show)
444451
self._name = 'GeoJson'
@@ -449,6 +456,7 @@ def __init__(self, data, style_function=None, highlight_function=None, # noqa
449456
self.smooth_factor = smooth_factor
450457
self.style = style_function is not None
451458
self.highlight = highlight_function is not None
459+
self.zoom_on_click = zoom_on_click
452460

453461
self.data = self.process_data(data)
454462

0 commit comments

Comments
 (0)