22
33from __future__ import (absolute_import , division , print_function )
44
5- from branca .element import CssLink , Figure , JavascriptLink , MacroElement
5+ from branca .element import CssLink , Figure , JavascriptLink
66
7- from folium .map import Icon , Marker , Popup
7+ from folium .map import Icon , Layer , Marker , Popup
88
99from jinja2 import Template
1010
1111
12- class MarkerCluster (MacroElement ):
12+ class MarkerCluster (Layer ):
1313 """
14- Creates a MarkerCluster plugin to append into a map with
15- Map.add_child.
14+ Provides Beautiful Animated Marker Clustering functionality for maps.
1615
1716 Parameters
1817 ----------
19- locations: list of list or array of shape (n,2).
18+ name : string, default None
19+ The name of the Layer, as it will appear in LayerControls
20+ overlay : bool, default False
21+ Adds the layer as an optional overlay (True) or the base layer (False).
22+ control : bool, default True
23+ Whether the Layer will be included in LayerControls
24+ icon_create_function : string, default None
25+ Override the default behaviour, making possible to customize
26+ markers colors and sizes.
27+
28+ locations: list of list or array of shape (n, 2).
2029 Data points of the form [[lat, lng]].
21-
2230 popups: list of length n.
2331 Popup for each marker.
24-
2532 icons: list of length n.
2633 Icon for each marker.
2734
28- """
29- def __init__ (self , locations , popups = None , icons = None ):
30- super (MarkerCluster , self ).__init__ ()
31- self ._name = 'MarkerCluster'
35+ Example
36+ -------
37+ >>> icon_create_function = '''
38+ ... function(cluster) {
39+ ... return L.divIcon({html: '<b>' + cluster.getChildCount() + '</b>',
40+ ... className: 'marker-cluster marker-cluster-small',
41+ ... iconSize: new L.Point(20, 20)});
42+ }'''
3243
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_child (Marker (location , popup = p , icon = i ))
44+ """
45+ def __init__ (self , locations = None , popups = None , icons = None , name = None ,
46+ overlay = True , control = True , icon_create_function = None ):
47+ super (MarkerCluster , self ).__init__ (name = name , overlay = overlay , control = control ) # noqa
48+
49+ if locations is not None :
50+ if popups is None :
51+ popups = [None ]* len (locations )
52+ if icons is None :
53+ icons = [None ]* len (locations )
54+ for location , popup , icon in zip (locations , popups , icons ):
55+ p = popup if popup is None or isinstance (popup , Popup ) else Popup (popup ) # noqa
56+ i = icon if icon is None or isinstance (icon , Icon ) else Icon (icon ) # noqa
57+ self .add_child (Marker (location , popup = p , icon = i ))
4858
59+ self ._name = 'MarkerCluster'
60+ self ._icon_create_function = icon_create_function .strip () if icon_create_function else '' # noqa
4961 self ._template = Template (u"""
5062 {% macro script(this, kwargs) %}
51- var {{this.get_name()}} = L.markerClusterGroup();
52- {{this._parent.get_name()}}.addLayer({{this.get_name()}});
63+ var {{this.get_name()}} = L.markerClusterGroup({
64+ {% if this._icon_create_function %}
65+ iconCreateFunction: {{this._icon_create_function}}
66+ {% endif %}
67+ });
68+ {{this._parent.get_name()}}.addLayer({{this.get_name()}});
5369 {% endmacro %}
5470 """ )
5571
@@ -61,13 +77,13 @@ def render(self, **kwargs):
6177 'if it is not in a Figure.' )
6278
6379 figure .header .add_child (
64- JavascriptLink ('https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.0 .0/leaflet.markercluster.js' ), # noqa
80+ JavascriptLink ('https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.1 .0/leaflet.markercluster.js' ), # noqa
6581 name = 'markerclusterjs' )
6682
6783 figure .header .add_child (
68- CssLink ('https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.0 .0/MarkerCluster.css' ), # noqa
84+ CssLink ('https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.1 .0/MarkerCluster.css' ), # noqa
6985 name = 'markerclustercss' )
7086
7187 figure .header .add_child (
72- CssLink ('https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.0 .0/MarkerCluster.Default.css' ), # noqa
88+ CssLink ('https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.1 .0/MarkerCluster.Default.css' ), # noqa
7389 name = 'markerclusterdefaultcss' )
0 commit comments