diff --git a/folium/features.py b/folium/features.py index 1c18f426db..f44840dc12 100644 --- a/folium/features.py +++ b/folium/features.py @@ -1718,7 +1718,6 @@ class DivIcon(MacroElement): """ {% macro script(this, kwargs) %} var {{ this.get_name() }} = L.divIcon({{ this.options|tojavascript }}); - {{this._parent.get_name()}}.setIcon({{this.get_name()}}); {% endmacro %} """ ) # noqa @@ -1895,7 +1894,6 @@ class CustomIcon(Icon): """ {% macro script(this, kwargs) %} var {{ this.get_name() }} = L.icon({{ this.options|tojavascript }}); - {{ this._parent.get_name() }}.setIcon({{ this.get_name() }}); {% endmacro %} """ ) # noqa diff --git a/folium/map.py b/folium/map.py index e922906713..77ec702a83 100644 --- a/folium/map.py +++ b/folium/map.py @@ -382,7 +382,9 @@ class SetIcon(MacroElement): """ ) - def __init__(self, marker: "Marker", icon: "Icon"): + def __init__( + self, marker: "Marker", icon: Union[Icon, "CustomIcon", "DivIcon"] + ): super().__init__() self._name = "SetIcon" self.marker = marker @@ -406,8 +408,7 @@ def __init__( # this attribute is not used by Marker, but by GeoJson self.icon = None if icon is not None: - self.add_child(icon) - self.icon = icon + self.set_icon(icon) if popup is not None: self.add_child(popup if isinstance(popup, Popup) else Popup(str(popup))) if tooltip is not None: @@ -432,6 +433,19 @@ def render(self): self.add_child(self.SetIcon(marker=self, icon=self.icon)) super().render() + def set_icon(self, icon): + """Set the icon for this Marker""" + super().add_child(icon) + self.icon = icon + + def add_child(self, child, name=None, index=None): + import folium.features as features + + if isinstance(child, (Icon, features.CustomIcon, features.DivIcon)): + self.set_icon(child) + else: + super().add_child(child, name, index) + class Popup(MacroElement): """Create a Popup instance that can be linked to a Layer.