Skip to content

Commit bdbf7ed

Browse files
authored
Multiple markers with one icon (#2068)
* wip * this works * also add customicon and divicon * run black
1 parent 7c8073e commit bdbf7ed

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

folium/map.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,6 @@ class Icon(MacroElement):
275275
var {{ this.get_name() }} = L.AwesomeMarkers.icon(
276276
{{ this.options|tojavascript }}
277277
);
278-
{{ this._parent.get_name() }}.setIcon({{ this.get_name() }});
279278
{% endmacro %}
280279
"""
281280
)
@@ -341,7 +340,7 @@ class Marker(MacroElement):
341340
folium.Popup or a folium.Popup instance.
342341
tooltip: str or folium.Tooltip, default None
343342
Display a text when hovering over the object.
344-
icon: Icon plugin
343+
icon: Icon, CustomIcon or DivIcon, optional
345344
the Icon plugin to use to render the marker.
346345
draggable: bool, default False
347346
Set to True to be able to drag the marker around the map.
@@ -372,6 +371,23 @@ class Marker(MacroElement):
372371
"""
373372
)
374373

374+
class SetIcon(MacroElement):
375+
"""Set the icon of a marker after both are created."""
376+
377+
_template = Template(
378+
"""
379+
{% macro script(this, kwargs) %}
380+
{{ this.marker.get_name() }}.setIcon({{ this.icon.get_name() }});
381+
{% endmacro %}
382+
"""
383+
)
384+
385+
def __init__(self, marker: "Marker", icon: "Icon"):
386+
super().__init__()
387+
self._name = "SetIcon"
388+
self.marker = marker
389+
self.icon = icon
390+
375391
def __init__(
376392
self,
377393
location: Optional[Sequence[float]] = None,
@@ -389,7 +405,6 @@ def __init__(
389405
)
390406
if icon is not None:
391407
self.add_child(icon)
392-
self.icon = icon
393408
if popup is not None:
394409
self.add_child(popup if isinstance(popup, Popup) else Popup(str(popup)))
395410
if tooltip is not None:
@@ -406,10 +421,15 @@ def _get_self_bounds(self) -> TypeBoundsReturn:
406421
return cast(TypeBoundsReturn, [self.location, self.location])
407422

408423
def render(self):
424+
from .features import CustomIcon, DivIcon
425+
409426
if self.location is None:
410427
raise ValueError(
411428
f"{self._name} location must be assigned when added directly to map."
412429
)
430+
for child in list(self._children.values()):
431+
if isinstance(child, (Icon, CustomIcon, DivIcon)):
432+
self.add_child(self.SetIcon(marker=self, icon=child))
413433
super().render()
414434

415435

0 commit comments

Comments
 (0)