From 84fb7393ab6f93e3fd8f50eaf89444f75405ba8b Mon Sep 17 00:00:00 2001 From: Rob Letzler Date: Thu, 2 Jan 2025 00:18:24 -0500 Subject: [PATCH] clone icons when adding; ensure unique parent-child addressing issue #1885 --- folium/map.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/folium/map.py b/folium/map.py index 36c4655945..ae9f38c7c9 100644 --- a/folium/map.py +++ b/folium/map.py @@ -2,6 +2,7 @@ Classes for drawing maps. """ +print("LOCAL FOLIUM IMPORTED") import warnings from collections import OrderedDict @@ -9,6 +10,10 @@ from branca.element import Element, Figure, Html, MacroElement +# +# +# + from folium.elements import ElementAddToElement, EventHandler from folium.template import Template from folium.utilities import ( @@ -22,6 +27,10 @@ validate_location, ) +#additional imports added for cloneIcon +from binascii import hexlify +from os import urandom + if TYPE_CHECKING: from folium.features import CustomIcon, DivIcon @@ -325,6 +334,15 @@ def __init__( extra_classes=f"fa-rotate-{angle}", **kwargs, ) + +#adding a function that can create a new copy of an icon/DivIcon/CustomIcon all of which extend MacroElement +#since the map needs to have a unique parent/child relationship with each icon +#reusing a single icon does not work +#It would be more elegant to put this in Branca +def cloneIcon(Icon_to_clone): + clone = copy.copy(Icon_to_clone) + clone._id = hexlify(urandom(16)).decode() + return clone class Marker(MacroElement): @@ -388,6 +406,11 @@ def __init__( draggable=draggable or None, autoPan=draggable or None, **kwargs ) if icon is not None: + #each icon needs to have a unique parent child relationship with the map + #we ensure that repeated calls with the same icon yield valid results by cloning it + #add child will overwrite any existing parent field value, so there's no need for us + # to test or reset it here. + icon = cloneIcon(icon) self.add_child(icon) self.icon = icon if popup is not None: