1
1
# -*- coding: utf-8 -*-
2
2
3
3
from __future__ import (absolute_import , division , print_function )
4
+ import json
4
5
5
6
from branca .element import CssLink , Figure , JavascriptLink
6
7
@@ -15,6 +16,12 @@ class MarkerCluster(Layer):
15
16
16
17
Parameters
17
18
----------
19
+ locations: list of list or array of shape (n, 2).
20
+ Data points of the form [[lat, lng]].
21
+ popups: list of length n, default None
22
+ Popup for each marker, either a Popup object or a string or None.
23
+ icons: list of length n, default None
24
+ Icon for each marker, either an Icon object or a string or None.
18
25
name : string, default None
19
26
The name of the Layer, as it will appear in LayerControls
20
27
overlay : bool, default True
@@ -26,53 +33,54 @@ class MarkerCluster(Layer):
26
33
icon_create_function : string, default None
27
34
Override the default behaviour, making possible to customize
28
35
markers colors and sizes.
29
-
30
- locations: list of list or array of shape (n, 2).
31
- Data points of the form [[lat, lng]].
32
- popups: list of length n.
33
- Popup for each marker.
34
- icons: list of length n.
35
- Icon for each marker.
36
+ options : dict, default None
37
+ A dictionary with options for Leaflet.markercluster. See
38
+ https://github.com/Leaflet/Leaflet.markercluster for options.
36
39
37
40
Example
38
41
-------
39
42
>>> icon_create_function = '''
40
- ... function(cluster) {
41
- ... return L.divIcon({html: '<b>' + cluster.getChildCount() + '</b>',
42
- ... className: 'marker-cluster marker-cluster-small',
43
- ... iconSize: new L.Point(20, 20)});
44
- }'''
43
+ ... function(cluster) {
44
+ ... return L.divIcon({html: '<b>' + cluster.getChildCount() + '</b>',
45
+ ... className: 'marker-cluster marker-cluster-small',
46
+ ... iconSize: new L.Point(20, 20)});
47
+ ... }
48
+ ... '''
45
49
46
50
"""
47
51
_template = Template (u"""
48
52
{% macro script(this, kwargs) %}
49
- var {{this.get_name()}} = L.markerClusterGroup({
50
- {% if this._icon_create_function %}
51
- iconCreateFunction: {{this._icon_create_function}}
52
- {% endif %}
53
- });
53
+ var {{this.get_name()}} = L.markerClusterGroup({{ this.options }});
54
54
{{this._parent.get_name()}}.addLayer({{this.get_name()}});
55
55
{% endmacro %}
56
56
""" )
57
57
58
58
def __init__ (self , locations = None , popups = None , icons = None , name = None ,
59
59
overlay = True , control = True , show = True ,
60
- icon_create_function = None ):
60
+ icon_create_function = None , options = None ):
61
61
super (MarkerCluster , self ).__init__ (name = name , overlay = overlay ,
62
62
control = control , show = show )
63
+ self ._name = 'MarkerCluster'
63
64
64
65
if locations is not None :
65
66
if popups is None :
66
- popups = [None ]* len (locations )
67
+ popups = [None ] * len (locations )
67
68
if icons is None :
68
- icons = [None ]* len (locations )
69
+ icons = [None ] * len (locations )
69
70
for location , popup , icon in zip (locations , popups , icons ):
70
- p = popup if popup is None or isinstance (popup , Popup ) else Popup (popup ) # noqa
71
- i = icon if icon is None or isinstance (icon , Icon ) else Icon (icon ) # noqa
71
+ p = popup if self . _validate (popup , Popup ) else Popup (popup )
72
+ i = icon if self . _validate (icon , Icon ) else Icon (icon )
72
73
self .add_child (Marker (location , popup = p , icon = i ))
73
74
74
- self ._name = 'MarkerCluster'
75
- self ._icon_create_function = icon_create_function .strip () if icon_create_function else '' # noqa
75
+ options = {} if options is None else options
76
+ if icon_create_function is not None :
77
+ options ['iconCreateFunction' ] = icon_create_function .strip ()
78
+ self .options = json .dumps (options , sort_keys = True , indent = 2 )
79
+
80
+ @staticmethod
81
+ def _validate (obj , cls ):
82
+ """Check whether the given object is from the given class or is None."""
83
+ return True if obj is None or isinstance (obj , cls ) else False
76
84
77
85
def render (self , ** kwargs ):
78
86
super (MarkerCluster , self ).render (** kwargs )
0 commit comments