From 6852dfef88632116f24ccbedaa2eea0f77aa1d8b Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Fri, 21 Mar 2025 10:47:26 +0100 Subject: [PATCH 1/4] make DivIcon and CustomIcon work with the new Marker approach --- folium/features.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/folium/features.py b/folium/features.py index 1c18f426db..f44840dc12 100644 --- a/folium/features.py +++ b/folium/features.py @@ -1718,7 +1718,6 @@ class DivIcon(MacroElement): """ {% macro script(this, kwargs) %} var {{ this.get_name() }} = L.divIcon({{ this.options|tojavascript }}); - {{this._parent.get_name()}}.setIcon({{this.get_name()}}); {% endmacro %} """ ) # noqa @@ -1895,7 +1894,6 @@ class CustomIcon(Icon): """ {% macro script(this, kwargs) %} var {{ this.get_name() }} = L.icon({{ this.options|tojavascript }}); - {{ this._parent.get_name() }}.setIcon({{ this.get_name() }}); {% endmacro %} """ ) # noqa From a53a7b7475e461e01b56a369bc4e68f4e2f1dfa6 Mon Sep 17 00:00:00 2001 From: Hans Then Date: Sun, 13 Apr 2025 09:00:09 +0200 Subject: [PATCH 2/4] Fixed typing --- folium/map.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/folium/map.py b/folium/map.py index e922906713..f2b8aa5aea 100644 --- a/folium/map.py +++ b/folium/map.py @@ -382,7 +382,9 @@ class SetIcon(MacroElement): """ ) - def __init__(self, marker: "Marker", icon: "Icon"): + def __init__( + self, marker: "Marker", icon: Union[Icon, "CustomIcon", "DivIcon"] + ): super().__init__() self._name = "SetIcon" self.marker = marker From d73fa2980e5717f12a0e78c004c63cd362b9478f Mon Sep 17 00:00:00 2001 From: Hans Then Date: Sun, 13 Apr 2025 20:20:09 +0200 Subject: [PATCH 3/4] Add icon also via add_to and add_child --- folium/map.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/folium/map.py b/folium/map.py index f2b8aa5aea..8347afdc3c 100644 --- a/folium/map.py +++ b/folium/map.py @@ -408,8 +408,7 @@ def __init__( # this attribute is not used by Marker, but by GeoJson self.icon = None if icon is not None: - self.add_child(icon) - self.icon = icon + self.set_icon(icon) if popup is not None: self.add_child(popup if isinstance(popup, Popup) else Popup(str(popup))) if tooltip is not None: @@ -434,6 +433,19 @@ def render(self): self.add_child(self.SetIcon(marker=self, icon=self.icon)) super().render() + def set_icon(self, icon: Union[Icon, "CustomIcon", "DivIcon"]): + """Set the icon for this Marker""" + super().add_child(icon) + self.icon = icon + + def add_child(self, child, name=None, index=None): + import folium.features as features + + if isinstance(child, (Icon, features.CustomIcon, features.DivIcon)): + self.set_icon(child) + else: + super().add_child(child, name, index) + class Popup(MacroElement): """Create a Popup instance that can be linked to a Layer. From 30d463368099b45d681f235d3cc37253346813f1 Mon Sep 17 00:00:00 2001 From: Hans Then Date: Sun, 13 Apr 2025 21:38:23 +0200 Subject: [PATCH 4/4] Remove type definition --- folium/map.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/folium/map.py b/folium/map.py index 8347afdc3c..77ec702a83 100644 --- a/folium/map.py +++ b/folium/map.py @@ -433,7 +433,7 @@ def render(self): self.add_child(self.SetIcon(marker=self, icon=self.icon)) super().render() - def set_icon(self, icon: Union[Icon, "CustomIcon", "DivIcon"]): + def set_icon(self, icon): """Set the icon for this Marker""" super().add_child(icon) self.icon = icon