Skip to content

Commit

Permalink
clone icons when adding; ensure unique parent-child
Browse files Browse the repository at this point in the history
addressing issue python-visualization#1885
  • Loading branch information
rl-utility-man authored Jan 2, 2025
1 parent 7c8073e commit 84fb739
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions folium/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
Classes for drawing maps.
"""
print("LOCAL FOLIUM IMPORTED")

import warnings
from collections import OrderedDict
from typing import TYPE_CHECKING, Optional, Sequence, Union, cast

from branca.element import Element, Figure, Html, MacroElement

#
#
#

from folium.elements import ElementAddToElement, EventHandler
from folium.template import Template
from folium.utilities import (
Expand All @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 84fb739

Please sign in to comment.