From 89b950823daa84c1934315d7ffba6a7c2c825a9e Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Sun, 29 Dec 2024 17:54:30 +0100 Subject: [PATCH 1/4] wip --- folium/map.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/folium/map.py b/folium/map.py index 36c465594..b86c96cd8 100644 --- a/folium/map.py +++ b/folium/map.py @@ -372,6 +372,20 @@ class Marker(MacroElement): """ ) + class SetIcon(MacroElement): + """Set the icon of a marker after both are created.""" + _template = Template(""" + {% macro script(this, kwargs) %} + {{ this.marker.get_name() }}.setIcon({{ this.icon.get_name() }}); + {% endmacro %} + """) + + def __init__(self, marker: 'Marker', icon: 'Icon'): + super().__init__() + self._name = "SetIcon" + self.marker = marker + self.icon = icon + def __init__( self, location: Optional[Sequence[float]] = None, @@ -388,7 +402,8 @@ def __init__( draggable=draggable or None, autoPan=draggable or None, **kwargs ) if icon is not None: - self.add_child(icon) + # this makes sure it is added only once + self._parent.add_child(icon, name=icon.get_name(), index=0) self.icon = icon if popup is not None: self.add_child(popup if isinstance(popup, Popup) else Popup(str(popup))) @@ -406,6 +421,8 @@ def _get_self_bounds(self) -> TypeBoundsReturn: return cast(TypeBoundsReturn, [self.location, self.location]) def render(self): + if self.icon: + self.add_child(self.SetIcon(marker=self, icon=self.icon)) if self.location is None: raise ValueError( f"{self._name} location must be assigned when added directly to map." From b9488ff0760d3d4684bebd184028348e7bd85316 Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Wed, 1 Jan 2025 16:46:39 +0100 Subject: [PATCH 2/4] this works --- folium/map.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/folium/map.py b/folium/map.py index b86c96cd8..960496c87 100644 --- a/folium/map.py +++ b/folium/map.py @@ -275,7 +275,6 @@ class Icon(MacroElement): var {{ this.get_name() }} = L.AwesomeMarkers.icon( {{ this.options|tojavascript }} ); - {{ this._parent.get_name() }}.setIcon({{ this.get_name() }}); {% endmacro %} """ ) @@ -402,9 +401,7 @@ def __init__( draggable=draggable or None, autoPan=draggable or None, **kwargs ) if icon is not None: - # this makes sure it is added only once - self._parent.add_child(icon, name=icon.get_name(), index=0) - self.icon = icon + self.add_child(icon) if popup is not None: self.add_child(popup if isinstance(popup, Popup) else Popup(str(popup))) if tooltip is not None: @@ -421,12 +418,13 @@ def _get_self_bounds(self) -> TypeBoundsReturn: return cast(TypeBoundsReturn, [self.location, self.location]) def render(self): - if self.icon: - self.add_child(self.SetIcon(marker=self, icon=self.icon)) if self.location is None: raise ValueError( f"{self._name} location must be assigned when added directly to map." ) + for child in list(self._children.values()): + if isinstance(child, Icon): + self.add_child(self.SetIcon(marker=self, icon=child)) super().render() From e7569a59ecf6137991cabd446c1e541ecdc9f215 Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Wed, 1 Jan 2025 16:56:13 +0100 Subject: [PATCH 3/4] also add customicon and divicon --- folium/map.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/folium/map.py b/folium/map.py index 960496c87..79c17e0aa 100644 --- a/folium/map.py +++ b/folium/map.py @@ -340,7 +340,7 @@ class Marker(MacroElement): folium.Popup or a folium.Popup instance. tooltip: str or folium.Tooltip, default None Display a text when hovering over the object. - icon: Icon plugin + icon: Icon, CustomIcon or DivIcon, optional the Icon plugin to use to render the marker. draggable: bool, default False Set to True to be able to drag the marker around the map. @@ -418,12 +418,14 @@ def _get_self_bounds(self) -> TypeBoundsReturn: return cast(TypeBoundsReturn, [self.location, self.location]) def render(self): + from .features import CustomIcon, DivIcon + if self.location is None: raise ValueError( f"{self._name} location must be assigned when added directly to map." ) for child in list(self._children.values()): - if isinstance(child, Icon): + if isinstance(child, (Icon, CustomIcon, DivIcon)): self.add_child(self.SetIcon(marker=self, icon=child)) super().render() From 2ce4fbeca2b33e0cd28dceadf7681e08b89469e3 Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Wed, 1 Jan 2025 17:04:24 +0100 Subject: [PATCH 4/4] run black --- folium/map.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/folium/map.py b/folium/map.py index 79c17e0aa..71bf8dcb3 100644 --- a/folium/map.py +++ b/folium/map.py @@ -373,13 +373,16 @@ class Marker(MacroElement): class SetIcon(MacroElement): """Set the icon of a marker after both are created.""" - _template = Template(""" + + _template = Template( + """ {% macro script(this, kwargs) %} {{ this.marker.get_name() }}.setIcon({{ this.icon.get_name() }}); {% endmacro %} - """) + """ + ) - def __init__(self, marker: 'Marker', icon: 'Icon'): + def __init__(self, marker: "Marker", icon: "Icon"): super().__init__() self._name = "SetIcon" self.marker = marker