@@ -372,6 +372,20 @@ class Marker(MacroElement):
372372 """
373373 )
374374
375+ class SetIcon (MacroElement ):
376+ """Set the icon of a marker after both are created."""
377+ _template = Template ("""
378+ {% macro script(this, kwargs) %}
379+ {{ this.marker.get_name() }}.setIcon({{ this.icon.get_name() }});
380+ {% endmacro %}
381+ """ )
382+
383+ def __init__ (self , marker : 'Marker' , icon : 'Icon' ):
384+ super ().__init__ ()
385+ self ._name = "SetIcon"
386+ self .marker = marker
387+ self .icon = icon
388+
375389 def __init__ (
376390 self ,
377391 location : Optional [Sequence [float ]] = None ,
@@ -388,7 +402,8 @@ def __init__(
388402 draggable = draggable or None , autoPan = draggable or None , ** kwargs
389403 )
390404 if icon is not None :
391- self .add_child (icon )
405+ # this makes sure it is added only once
406+ self ._parent .add_child (icon , name = icon .get_name (), index = 0 )
392407 self .icon = icon
393408 if popup is not None :
394409 self .add_child (popup if isinstance (popup , Popup ) else Popup (str (popup )))
@@ -406,6 +421,8 @@ def _get_self_bounds(self) -> TypeBoundsReturn:
406421 return cast (TypeBoundsReturn , [self .location , self .location ])
407422
408423 def render (self ):
424+ if self .icon :
425+ self .add_child (self .SetIcon (marker = self , icon = self .icon ))
409426 if self .location is None :
410427 raise ValueError (
411428 f"{ self ._name } location must be assigned when added directly to map."
0 commit comments