|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from jinja2 import Template |
| 4 | + |
| 5 | +from folium.elements import Element, JSCSSMixin, MacroElement |
| 6 | +from folium.map import Marker |
| 7 | +from folium.utilities import parse_options |
| 8 | + |
| 9 | + |
| 10 | +class OverlappingMarkerSpiderfier(JSCSSMixin, MacroElement): |
| 11 | + """ |
| 12 | + A plugin that handles overlapping markers on a map by spreading them out in a spiral or circle pattern when clicked. |
| 13 | +
|
| 14 | + This plugin is useful when you have multiple markers in close proximity that would otherwise be difficult to interact with. |
| 15 | + When a user clicks on a cluster of overlapping markers, they spread out in a 'spider' pattern, making each marker |
| 16 | + individually accessible. |
| 17 | +
|
| 18 | + Markers are automatically identified and managed by the plugin, so there is no need to add them separately. |
| 19 | + Simply add the plugin to the map using `oms.add_to(map)`. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + keep_spiderfied : bool, default True |
| 24 | + If true, markers stay spiderfied after clicking. |
| 25 | + nearby_distance : int, default 20 |
| 26 | + Pixels away from a marker that is considered overlapping. |
| 27 | + leg_weight : float, default 1.5 |
| 28 | + Weight of the spider legs. |
| 29 | + circle_spiral_switchover : int, default 9 |
| 30 | + Number of markers at which to switch from circle to spiral pattern. |
| 31 | +
|
| 32 | + Example |
| 33 | + ------- |
| 34 | + >>> oms = OverlappingMarkerSpiderfier( |
| 35 | + ... keep_spiderfied=True, nearby_distance=30, leg_weight=2.0 |
| 36 | + ... ) |
| 37 | + >>> oms.add_to(map) |
| 38 | + """ |
| 39 | + |
| 40 | + _template = Template( |
| 41 | + """ |
| 42 | + {% macro script(this, kwargs) %} |
| 43 | + (function () { |
| 44 | + try { |
| 45 | + var oms = new OverlappingMarkerSpiderfier( |
| 46 | + {{ this._parent.get_name() }}, |
| 47 | + {{ this.options|tojson }} |
| 48 | + ); |
| 49 | +
|
| 50 | + oms.addListener('spiderfy', function() { |
| 51 | + {{ this._parent.get_name() }}.closePopup(); |
| 52 | + }); |
| 53 | +
|
| 54 | + {%- for marker in this.markers %} |
| 55 | + oms.addMarker({{ marker.get_name() }}); |
| 56 | + {%- endfor %} |
| 57 | + } catch (error) { |
| 58 | + console.error('Error initializing OverlappingMarkerSpiderfier:', error); |
| 59 | + } |
| 60 | + })(); |
| 61 | + {% endmacro %} |
| 62 | + """ |
| 63 | + ) |
| 64 | + |
| 65 | + default_js = [ |
| 66 | + ( |
| 67 | + "overlappingmarkerjs", |
| 68 | + "https://cdnjs.cloudflare.com/ajax/libs/OverlappingMarkerSpiderfier-Leaflet/0.2.6/oms.min.js", |
| 69 | + ) |
| 70 | + ] |
| 71 | + |
| 72 | + def __init__( |
| 73 | + self, |
| 74 | + keep_spiderfied: bool = True, |
| 75 | + nearby_distance: int = 20, |
| 76 | + leg_weight: float = 1.5, |
| 77 | + circle_spiral_switchover: int = 9, |
| 78 | + **kwargs |
| 79 | + ): |
| 80 | + super().__init__() |
| 81 | + self._name = "OverlappingMarkerSpiderfier" |
| 82 | + self.options = parse_options( |
| 83 | + keep_spiderfied=keep_spiderfied, |
| 84 | + nearby_distance=nearby_distance, |
| 85 | + leg_weight=leg_weight, |
| 86 | + circle_spiral_switchover=circle_spiral_switchover, |
| 87 | + **kwargs |
| 88 | + ) |
| 89 | + |
| 90 | + def add_to( |
| 91 | + self, parent: Element, name: Optional[str] = None, index: Optional[int] = None |
| 92 | + ) -> Element: |
| 93 | + self._parent = parent |
| 94 | + self.markers = self._get_all_markers(parent) |
| 95 | + super().add_to(parent, name=name, index=index) |
| 96 | + |
| 97 | + def _get_all_markers(self, element: Element) -> list: |
| 98 | + markers = [] |
| 99 | + for child in element._children.values(): |
| 100 | + if isinstance(child, Marker): |
| 101 | + markers.append(child) |
| 102 | + elif hasattr(child, "_children"): |
| 103 | + markers.extend(self._get_all_markers(child)) |
| 104 | + return markers |
0 commit comments