Skip to content
Open
Show file tree
Hide file tree
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
25 changes: 15 additions & 10 deletions folium/plugins/overlapping_marker_spiderfier.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from jinja2 import Template

from folium.elements import Element, JSCSSMixin, MacroElement
from folium.folium import Map
from folium.map import Marker
from folium.utilities import parse_options
from folium.utilities import get_obj_in_upper_tree, parse_options


class OverlappingMarkerSpiderfier(JSCSSMixin, MacroElement):
Expand All @@ -14,7 +15,9 @@ class OverlappingMarkerSpiderfier(JSCSSMixin, MacroElement):
individually accessible.

Markers are automatically identified and managed by the plugin, so there is no need to add them separately.
Simply add the plugin to the map using `oms.add_to(map)`.
Simply add the plugin to the map using `oms.add_to(map)`. All markers below the
plugin's parent are handled, so add it to a FeatureGroup to only spiderfy the
markers in that group. Add the plugin after the markers it should handle.

Parameters
----------
Expand All @@ -40,12 +43,12 @@ class OverlappingMarkerSpiderfier(JSCSSMixin, MacroElement):
(function () {
try {
var oms = new OverlappingMarkerSpiderfier(
{{ this._parent.get_name() }},
{{ this.map.get_name() }},
{{ this.options|tojson }}
);

oms.addListener('spiderfy', function() {
{{ this._parent.get_name() }}.closePopup();
{{ this.map.get_name() }}.closePopup();
});

{%- for marker in this.markers %}
Expand Down Expand Up @@ -82,13 +85,15 @@ def __init__(
circle_spiral_switchover=circle_spiral_switchover,
**kwargs,
)
self.map: Map | None = None
self.markers: list[Marker] = []

def add_to(
self, parent: Element, name: str | None = None, index: int | None = None
) -> Element:
self._parent = parent
self.markers = self._get_all_markers(parent)
return super().add_to(parent, name=name, index=index)
def render(self, **kwargs):
# Look the markers up now rather than when the plugin is added, so it
# also works with `add_child` and with markers added to a group later.
self.map = get_obj_in_upper_tree(self, Map)
self.markers = self._get_all_markers(self._parent)
super().render(**kwargs)

def _get_all_markers(self, element: Element) -> list:
markers = []
Expand Down
43 changes: 42 additions & 1 deletion tests/plugins/test_overlapping_marker_spiderfier.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import numpy as np

from folium.folium import Map
from folium.map import Marker
from folium.map import FeatureGroup, Marker
from folium.plugins.overlapping_marker_spiderfier import OverlappingMarkerSpiderfier
from folium.utilities import normalize


def test_oms_js_inclusion():
Expand Down Expand Up @@ -113,3 +114,43 @@ def test_overlapping_marker_spiderfier_integration():
assert (
oms.get_name() in m._children
), "OverlappingMarkerSpiderfier is not correctly added to the map."


def test_markers_found_when_added_with_add_child():
m = Map([45.05, 3.05], zoom_start=14)
markers = [Marker([45.05, 3.05]).add_to(m) for _ in range(3)]
oms = OverlappingMarkerSpiderfier()
m.add_child(oms)

out = normalize(m.get_root().render())

for marker in markers:
assert f"oms.addMarker({marker.get_name()});" in out


def test_markers_added_to_group_after_plugin_are_found():
m = Map([45.05, 3.05], zoom_start=14)
group = FeatureGroup().add_to(m)
OverlappingMarkerSpiderfier().add_to(m)
marker = Marker([45.05, 3.05]).add_to(group)

out = normalize(m.get_root().render())

assert f"oms.addMarker({marker.get_name()});" in out


def test_added_to_feature_group_uses_the_map():
m = Map([45.05, 3.05], zoom_start=14)
group = FeatureGroup().add_to(m)
inside = [Marker([45.05, 3.05]).add_to(group) for _ in range(2)]
outside = Marker([45.05, 3.05]).add_to(m)
OverlappingMarkerSpiderfier().add_to(group)

out = normalize(m.get_root().render())

assert f"new OverlappingMarkerSpiderfier({m.get_name()}," in out
assert f"{m.get_name()}.closePopup();" in out
assert f"new OverlappingMarkerSpiderfier({group.get_name()}," not in out
for marker in inside:
assert f"oms.addMarker({marker.get_name()});" in out
assert f"oms.addMarker({outside.get_name()});" not in out
Loading