Skip to content

Commit d1df1c2

Browse files
authored
Merge pull request #2132 from hansthen/divicon-customicon-remove-seticon
Make DivIcon and CustomIcon work with the new Marker approach
2 parents 266bf74 + 30d4633 commit d1df1c2

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

folium/features.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,7 +1718,6 @@ class DivIcon(MacroElement):
17181718
"""
17191719
{% macro script(this, kwargs) %}
17201720
var {{ this.get_name() }} = L.divIcon({{ this.options|tojavascript }});
1721-
{{this._parent.get_name()}}.setIcon({{this.get_name()}});
17221721
{% endmacro %}
17231722
"""
17241723
) # noqa
@@ -1895,7 +1894,6 @@ class CustomIcon(Icon):
18951894
"""
18961895
{% macro script(this, kwargs) %}
18971896
var {{ this.get_name() }} = L.icon({{ this.options|tojavascript }});
1898-
{{ this._parent.get_name() }}.setIcon({{ this.get_name() }});
18991897
{% endmacro %}
19001898
"""
19011899
) # noqa

folium/map.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,9 @@ class SetIcon(MacroElement):
382382
"""
383383
)
384384

385-
def __init__(self, marker: "Marker", icon: "Icon"):
385+
def __init__(
386+
self, marker: "Marker", icon: Union[Icon, "CustomIcon", "DivIcon"]
387+
):
386388
super().__init__()
387389
self._name = "SetIcon"
388390
self.marker = marker
@@ -406,8 +408,7 @@ def __init__(
406408
# this attribute is not used by Marker, but by GeoJson
407409
self.icon = None
408410
if icon is not None:
409-
self.add_child(icon)
410-
self.icon = icon
411+
self.set_icon(icon)
411412
if popup is not None:
412413
self.add_child(popup if isinstance(popup, Popup) else Popup(str(popup)))
413414
if tooltip is not None:
@@ -432,6 +433,19 @@ def render(self):
432433
self.add_child(self.SetIcon(marker=self, icon=self.icon))
433434
super().render()
434435

436+
def set_icon(self, icon):
437+
"""Set the icon for this Marker"""
438+
super().add_child(icon)
439+
self.icon = icon
440+
441+
def add_child(self, child, name=None, index=None):
442+
import folium.features as features
443+
444+
if isinstance(child, (Icon, features.CustomIcon, features.DivIcon)):
445+
self.set_icon(child)
446+
else:
447+
super().add_child(child, name, index)
448+
435449

436450
class Popup(MacroElement):
437451
"""Create a Popup instance that can be linked to a Layer.

0 commit comments

Comments
 (0)