|
5 | 5 |
|
6 | 6 | Creates a MarkerCluster plugin to add on a folium map. |
7 | 7 | """ |
8 | | -import json |
| 8 | +#import json |
| 9 | +from jinja2 import Template |
9 | 10 |
|
10 | | -from .plugin import Plugin |
| 11 | +from folium.element import JavascriptLink, CssLink, MacroElement, Figure |
| 12 | +from folium.map import Popup, Icon, Marker |
11 | 13 |
|
12 | | -class MarkerCluster(Plugin): |
13 | | - """Adds a MarkerCluster layer on the map.""" |
14 | | - def __init__(self, data): |
| 14 | +class MarkerCluster(MacroElement): |
| 15 | + def __init__(self, locations, popups=None, icons=None): |
15 | 16 | """Creates a MarkerCluster plugin to append into a map with |
16 | | - Map.add_plugin. |
| 17 | + Map.add_children. |
17 | 18 |
|
18 | 19 | Parameters |
19 | 20 | ---------- |
20 | | - data: list of list or array of shape (n,3). |
21 | | - Data points of the form [[lat, lng, popup]]. |
| 21 | + locations: list of list or array of shape (n,2). |
| 22 | + Data points of the form [[lat, lng]]. |
| 23 | +
|
| 24 | + popups: list of length n. |
| 25 | + Popup for each marker. |
| 26 | +
|
| 27 | + icons: list of length n. |
| 28 | + Icon for each marker. |
22 | 29 | """ |
23 | 30 | super(MarkerCluster, self).__init__() |
24 | | - self.plugin_name = 'MarkerCluster' |
25 | | - self.data = [tuple(x) for x in data] |
26 | | - |
27 | | - def render_header(self, nb): |
28 | | - """Generates the HTML part of the plugin.""" |
29 | | - return """ |
30 | | - <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/MarkerCluster.css" /> |
31 | | - <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/MarkerCluster.Default.css" /> |
32 | | - <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/leaflet.markercluster.js"></script> |
33 | | - """ if nb==0 else "" |
34 | | - |
35 | | - def render_js(self, nb): |
36 | | - """Generates the Javascript part of the plugin.""" |
37 | | - out = """ |
38 | | - var addressPoints = """+json.dumps(self.data)+"""; |
39 | | -
|
40 | | - var markers = L.markerClusterGroup(); |
41 | | -
|
42 | | - for (var i = 0; i < addressPoints.length; i++) { |
43 | | - var a = addressPoints[i]; |
44 | | - var title = a[2]; |
45 | | - var marker = L.marker(new L.LatLng(a[0], a[1]), { title: title }); |
46 | | - marker.bindPopup(title); |
47 | | - markers.addLayer(marker); |
48 | | - } |
49 | | -
|
50 | | - map.addLayer(markers); |
51 | | - """ |
52 | | - return out |
| 31 | + self._name = 'MarkerCluster' |
| 32 | + |
| 33 | + if popups is None: |
| 34 | + popups = [None]*len(locations) |
| 35 | + if icons is None: |
| 36 | + icons = [None]*len(locations) |
| 37 | + |
| 38 | + for location, popup, icon in zip(locations,popups,icons): |
| 39 | + if popup is None or isinstance(popup, Popup): |
| 40 | + p = popup |
| 41 | + else: |
| 42 | + p = Popup(popup) |
| 43 | + if icon is None or isinstance(icon, Icon): |
| 44 | + i = icon |
| 45 | + else: |
| 46 | + i = Icon(icon) |
| 47 | + self.add_children(Marker(location, popup=p, icon=i)) |
| 48 | + |
| 49 | + self._template = Template(u""" |
| 50 | + {% macro script(this, kwargs) %} |
| 51 | + var {{this.get_name()}} = L.markerClusterGroup(); |
| 52 | + {{this._parent.get_name()}}.addLayer({{this.get_name()}}); |
| 53 | + {% endmacro %} |
| 54 | + """) |
| 55 | + def render(self,**kwargs): |
| 56 | + super(MarkerCluster,self).render(**kwargs) |
| 57 | + |
| 58 | + figure = self.get_root() |
| 59 | + assert isinstance(figure,Figure), ("You cannot render this Element " |
| 60 | + "if it's not in a Figure.") |
| 61 | + |
| 62 | + figure.header.add_children(\ |
| 63 | + JavascriptLink("https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/leaflet.markercluster.js"), |
| 64 | + name='markerclusterjs') |
| 65 | + figure.header.add_children(\ |
| 66 | + CssLink("https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/MarkerCluster.css"), |
| 67 | + name='markerclustercss') |
| 68 | + figure.header.add_children(\ |
| 69 | + CssLink("https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/MarkerCluster.Default.css"), |
| 70 | + name='markerclusterdefaultcss') |
0 commit comments