Skip to content

Commit 88214f4

Browse files
committed
multiple markers with one icon
1 parent d04d4ba commit 88214f4

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

folium/map.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,6 @@ class Icon(MacroElement):
271271
var {{ this.get_name() }} = L.AwesomeMarkers.icon(
272272
{{ this.options|tojavascript }}
273273
);
274-
{{ this._parent.get_name() }}.setIcon({{ this.get_name() }});
275274
{% endmacro %}
276275
"""
277276
)
@@ -322,6 +321,24 @@ def __init__(
322321
**kwargs,
323322
)
324323

324+
class SetIcon(MacroElement):
325+
"""Set the icon of a marker after both are created."""
326+
_template = Template("""
327+
{% macro script(this, kwargs) %}
328+
{{ this.marker.get_name() }}.setIcon({{ this.icon.get_name() }});
329+
{% endmacro %}
330+
""")
331+
332+
def __init__(self, marker: 'Marker', icon: 'Icon'):
333+
super().__init__()
334+
self._name = "SetIcon"
335+
self.marker = marker
336+
self.icon = icon
337+
338+
def register_marker(self, marker: "Marker") -> None:
339+
"""Register a marker to use this icon, so one icon can have multiple markers."""
340+
self.add_child(Icon.SetIcon(marker=marker, icon=self))
341+
325342

326343
class Marker(MacroElement):
327344
"""
@@ -383,7 +400,11 @@ def __init__(
383400
self.options = remove_empty(
384401
draggable=draggable or None, autoPan=draggable or None, **kwargs
385402
)
403+
self.icon: Optional[Icon] = None
386404
if icon is not None:
405+
# here we make sure the icon is rendered after the last marker that needs it
406+
if icon._parent is not None:
407+
del icon._parent._children[icon.get_name()]
387408
self.add_child(icon)
388409
self.icon = icon
389410
if popup is not None:
@@ -406,6 +427,8 @@ def render(self) -> None:
406427
raise ValueError(
407428
f"{self._name} location must be assigned when added directly to map."
408429
)
430+
if self.icon:
431+
self.icon.register_marker(self)
409432
super().render()
410433

411434

0 commit comments

Comments
 (0)