Skip to content

Commit

Permalink
multiple markers with one icon
Browse files Browse the repository at this point in the history
  • Loading branch information
Conengmo committed Dec 14, 2024
1 parent d04d4ba commit 88214f4
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion folium/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ class Icon(MacroElement):
var {{ this.get_name() }} = L.AwesomeMarkers.icon(
{{ this.options|tojavascript }}
);
{{ this._parent.get_name() }}.setIcon({{ this.get_name() }});
{% endmacro %}
"""
)
Expand Down Expand Up @@ -322,6 +321,24 @@ def __init__(
**kwargs,
)

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 register_marker(self, marker: "Marker") -> None:
"""Register a marker to use this icon, so one icon can have multiple markers."""
self.add_child(Icon.SetIcon(marker=marker, icon=self))


class Marker(MacroElement):
"""
Expand Down Expand Up @@ -383,7 +400,11 @@ def __init__(
self.options = remove_empty(
draggable=draggable or None, autoPan=draggable or None, **kwargs
)
self.icon: Optional[Icon] = None
if icon is not None:
# here we make sure the icon is rendered after the last marker that needs it
if icon._parent is not None:
del icon._parent._children[icon.get_name()]
self.add_child(icon)
self.icon = icon
if popup is not None:
Expand All @@ -406,6 +427,8 @@ def render(self) -> None:
raise ValueError(
f"{self._name} location must be assigned when added directly to map."
)
if self.icon:
self.icon.register_marker(self)
super().render()


Expand Down

0 comments on commit 88214f4

Please sign in to comment.