Skip to content

Issue 1770: Add on_each_feature handler to GeoJson #2136

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

Merged
merged 1 commit into from
Apr 22, 2025
Merged
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
11 changes: 11 additions & 0 deletions folium/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from folium.map import FeatureGroup, Icon, Layer, Marker, Popup, Tooltip
from folium.template import Template
from folium.utilities import (
JsCode,
TypeBoundsReturn,
TypeContainer,
TypeJsonValue,
Expand Down Expand Up @@ -516,6 +517,10 @@ class GeoJson(Layer):
embedding is only supported if you provide a file link or URL.
zoom_on_click: bool, default False
Set to True to enable zooming in on a geometry when clicking on it.
on_each_feature: JsCode, optional
Javascript code to be called on each feature.
See https://leafletjs.com/examples/geojson/
`onEachFeature` for more information.
**kwargs
Keyword arguments are passed to the geoJson object as extra options.

Expand Down Expand Up @@ -585,6 +590,10 @@ class GeoJson(Layer):
{%- endif %}

function {{this.get_name()}}_onEachFeature(feature, layer) {
{%- if this.on_each_feature %}
({{this.on_each_feature}})(feature, layer);
{%- endif %}

layer.on({
{%- if this.highlight %}
mouseout: function(e) {
Expand Down Expand Up @@ -679,6 +688,7 @@ def __init__(
embed: bool = True,
popup: Optional["GeoJsonPopup"] = None,
zoom_on_click: bool = False,
on_each_feature: Optional[JsCode] = None,
marker: Union[Circle, CircleMarker, Marker, None] = None,
**kwargs: Any,
):
Expand All @@ -705,6 +715,7 @@ def __init__(
self.popup_keep_highlighted = popup_keep_highlighted

self.marker = marker
self.on_each_feature = on_each_feature
self.options = remove_empty(**kwargs)

self.data = self.process_data(data)
Expand Down