Skip to content

Commit de2729f

Browse files
authored
Merge pull request #837 from Conengmo/marker-cluster-options
Added options for MarkerCluster and FastMarkerCluster
2 parents 19bde41 + 0c1db9c commit de2729f

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

folium/plugins/fast_marker_cluster.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class FastMarkerCluster(MarkerCluster):
3737
Whether the Layer will be included in LayerControls.
3838
show: bool, default True
3939
Whether the layer will be shown on opening (only for overlays).
40+
options : dict, default None
41+
A dictionary with options for Leaflet.markercluster. See
42+
https://github.com/Leaflet/Leaflet.markercluster for options.
4043
4144
"""
4245
_template = Template(u"""
@@ -46,7 +49,7 @@ class FastMarkerCluster(MarkerCluster):
4649
{{this._callback}}
4750
4851
var data = {{ this._data }};
49-
var cluster = L.markerClusterGroup();
52+
var cluster = L.markerClusterGroup({{ this.options }});
5053
5154
for (var i = 0; i < data.length; i++) {
5255
var row = data[i];
@@ -59,10 +62,11 @@ class FastMarkerCluster(MarkerCluster):
5962
})();
6063
{% endmacro %}""")
6164

62-
def __init__(self, data, callback=None,
65+
def __init__(self, data, callback=None, options=None,
6366
name=None, overlay=True, control=True, show=True):
6467
super(FastMarkerCluster, self).__init__(name=name, overlay=overlay,
65-
control=control, show=show)
68+
control=control, show=show,
69+
options=options)
6670
self._name = 'FastMarkerCluster'
6771
self._data = _validate_coordinates(data)
6872

folium/plugins/marker_cluster.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
from __future__ import (absolute_import, division, print_function)
4+
import json
45

56
from branca.element import CssLink, Figure, JavascriptLink
67

@@ -15,6 +16,12 @@ class MarkerCluster(Layer):
1516
1617
Parameters
1718
----------
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.
1825
name : string, default None
1926
The name of the Layer, as it will appear in LayerControls
2027
overlay : bool, default True
@@ -26,53 +33,54 @@ class MarkerCluster(Layer):
2633
icon_create_function : string, default None
2734
Override the default behaviour, making possible to customize
2835
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.
3639
3740
Example
3841
-------
3942
>>> 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+
... '''
4549
4650
"""
4751
_template = Template(u"""
4852
{% 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 }});
5454
{{this._parent.get_name()}}.addLayer({{this.get_name()}});
5555
{% endmacro %}
5656
""")
5757

5858
def __init__(self, locations=None, popups=None, icons=None, name=None,
5959
overlay=True, control=True, show=True,
60-
icon_create_function=None):
60+
icon_create_function=None, options=None):
6161
super(MarkerCluster, self).__init__(name=name, overlay=overlay,
6262
control=control, show=show)
63+
self._name = 'MarkerCluster'
6364

6465
if locations is not None:
6566
if popups is None:
66-
popups = [None]*len(locations)
67+
popups = [None] * len(locations)
6768
if icons is None:
68-
icons = [None]*len(locations)
69+
icons = [None] * len(locations)
6970
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)
7273
self.add_child(Marker(location, popup=p, icon=i))
7374

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
7684

7785
def render(self, **kwargs):
7886
super(MarkerCluster, self).render(**kwargs)

0 commit comments

Comments
 (0)