Skip to content

Commit 764c691

Browse files
authored
Add on_each_feature handler to GeoJson (#2136)
This will allow users to customize the Tooltips and Popups for the GeoJson data. It should also help with issue #1520. Testing was done manually, using a streamlit program.
1 parent 1043da8 commit 764c691

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

folium/features.py

+11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from folium.map import FeatureGroup, Icon, Layer, Marker, Popup, Tooltip
2929
from folium.template import Template
3030
from folium.utilities import (
31+
JsCode,
3132
TypeBoundsReturn,
3233
TypeContainer,
3334
TypeJsonValue,
@@ -516,6 +517,10 @@ class GeoJson(Layer):
516517
embedding is only supported if you provide a file link or URL.
517518
zoom_on_click: bool, default False
518519
Set to True to enable zooming in on a geometry when clicking on it.
520+
on_each_feature: JsCode, optional
521+
Javascript code to be called on each feature.
522+
See https://leafletjs.com/examples/geojson/
523+
`onEachFeature` for more information.
519524
**kwargs
520525
Keyword arguments are passed to the geoJson object as extra options.
521526
@@ -585,6 +590,10 @@ class GeoJson(Layer):
585590
{%- endif %}
586591
587592
function {{this.get_name()}}_onEachFeature(feature, layer) {
593+
{%- if this.on_each_feature %}
594+
({{this.on_each_feature}})(feature, layer);
595+
{%- endif %}
596+
588597
layer.on({
589598
{%- if this.highlight %}
590599
mouseout: function(e) {
@@ -679,6 +688,7 @@ def __init__(
679688
embed: bool = True,
680689
popup: Optional["GeoJsonPopup"] = None,
681690
zoom_on_click: bool = False,
691+
on_each_feature: Optional[JsCode] = None,
682692
marker: Union[Circle, CircleMarker, Marker, None] = None,
683693
**kwargs: Any,
684694
):
@@ -705,6 +715,7 @@ def __init__(
705715
self.popup_keep_highlighted = popup_keep_highlighted
706716

707717
self.marker = marker
718+
self.on_each_feature = on_each_feature
708719
self.options = remove_empty(**kwargs)
709720

710721
self.data = self.process_data(data)

0 commit comments

Comments
 (0)