Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility for markers to link to other markers #1563 #1863

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions folium/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ def __init__(
show: bool = False,
sticky: bool = False,
lazy: bool = False,
linked_marker: Optional[Marker] = None,
linked_marker_name: str = "",
**kwargs: TypeJsonValue,
):
super().__init__()
Expand Down Expand Up @@ -464,6 +466,8 @@ def __init__(
closeOnClick=False if sticky else None,
**kwargs,
)
self.linked_marker = linked_marker
self.linked_marker_name = linked_marker_name

def render(self, **kwargs) -> None:
"""Renders the HTML representation of the element."""
Expand All @@ -480,6 +484,22 @@ def render(self, **kwargs) -> None:
name=self.get_name(),
)

if self.linked_marker is not None:
linking_script = f"""
var linkElement = document.createElement('a');
linkElement.href = '#';
linkElement.innerHTML = '{'Open ' + self.linked_marker_name if self.linked_marker_name else 'Open another popup'}';
linkElement.addEventListener('click', function() {{
{self._parent.get_name()}.closePopup();
{self.linked_marker.get_name()}.openPopup();
{self.linked_marker.get_name()}._map.flyTo({self.linked_marker.get_name()}.getLatLng());
}});
var popupContent = {self.get_name()}.getContent();
popupContent.appendChild(linkElement);
{self.get_name()}.setContent(popupContent);
"""
figure.script.add_child(Element(linking_script))


class Tooltip(MacroElement):
"""
Expand Down