Skip to content

Added options for MarkerCluster and FastMarkerCluster #837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions folium/plugins/fast_marker_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class FastMarkerCluster(MarkerCluster):
Whether the Layer will be included in LayerControls.
show: bool, default True
Whether the layer will be shown on opening (only for overlays).
options : dict, default None
A dictionary with options for Leaflet.markercluster. See
https://github.com/Leaflet/Leaflet.markercluster for options.

"""
_template = Template(u"""
Expand All @@ -46,7 +49,7 @@ class FastMarkerCluster(MarkerCluster):
{{this._callback}}

var data = {{ this._data }};
var cluster = L.markerClusterGroup();
var cluster = L.markerClusterGroup({{ this.options }});

for (var i = 0; i < data.length; i++) {
var row = data[i];
Expand All @@ -59,10 +62,11 @@ class FastMarkerCluster(MarkerCluster):
})();
{% endmacro %}""")

def __init__(self, data, callback=None,
def __init__(self, data, callback=None, options=None,
name=None, overlay=True, control=True, show=True):
super(FastMarkerCluster, self).__init__(name=name, overlay=overlay,
control=control, show=show)
control=control, show=show,
options=options)
self._name = 'FastMarkerCluster'
self._data = _validate_coordinates(data)

Expand Down
56 changes: 32 additions & 24 deletions folium/plugins/marker_cluster.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function)
import json

from branca.element import CssLink, Figure, JavascriptLink

Expand All @@ -15,6 +16,12 @@ class MarkerCluster(Layer):

Parameters
----------
locations: list of list or array of shape (n, 2).
Data points of the form [[lat, lng]].
popups: list of length n, default None
Popup for each marker, either a Popup object or a string or None.
icons: list of length n, default None
Icon for each marker, either an Icon object or a string or None.
name : string, default None
The name of the Layer, as it will appear in LayerControls
overlay : bool, default True
Expand All @@ -26,53 +33,54 @@ class MarkerCluster(Layer):
icon_create_function : string, default None
Override the default behaviour, making possible to customize
markers colors and sizes.

locations: list of list or array of shape (n, 2).
Data points of the form [[lat, lng]].
popups: list of length n.
Popup for each marker.
icons: list of length n.
Icon for each marker.
options : dict, default None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should look into a docstring template functionality to avoid these repetitions.
I believe pandas has a nice example of that, I'll look into it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I don't know how to do that yet, but I'm interested to know.

A dictionary with options for Leaflet.markercluster. See
https://github.com/Leaflet/Leaflet.markercluster for options.

Example
-------
>>> icon_create_function = '''
... function(cluster) {
... return L.divIcon({html: '<b>' + cluster.getChildCount() + '</b>',
... className: 'marker-cluster marker-cluster-small',
... iconSize: new L.Point(20, 20)});
}'''
... function(cluster) {
... return L.divIcon({html: '<b>' + cluster.getChildCount() + '</b>',
... className: 'marker-cluster marker-cluster-small',
... iconSize: new L.Point(20, 20)});
... }
... '''

"""
_template = Template(u"""
{% macro script(this, kwargs) %}
var {{this.get_name()}} = L.markerClusterGroup({
{% if this._icon_create_function %}
iconCreateFunction: {{this._icon_create_function}}
{% endif %}
});
var {{this.get_name()}} = L.markerClusterGroup({{ this.options }});
{{this._parent.get_name()}}.addLayer({{this.get_name()}});
{% endmacro %}
""")

def __init__(self, locations=None, popups=None, icons=None, name=None,
overlay=True, control=True, show=True,
icon_create_function=None):
icon_create_function=None, options=None):
super(MarkerCluster, self).__init__(name=name, overlay=overlay,
control=control, show=show)
self._name = 'MarkerCluster'

if locations is not None:
if popups is None:
popups = [None]*len(locations)
popups = [None] * len(locations)
if icons is None:
icons = [None]*len(locations)
icons = [None] * len(locations)
for location, popup, icon in zip(locations, popups, icons):
p = popup if popup is None or isinstance(popup, Popup) else Popup(popup) # noqa
i = icon if icon is None or isinstance(icon, Icon) else Icon(icon) # noqa
p = popup if self._validate(popup, Popup) else Popup(popup)
i = icon if self._validate(icon, Icon) else Icon(icon)
self.add_child(Marker(location, popup=p, icon=i))

self._name = 'MarkerCluster'
self._icon_create_function = icon_create_function.strip() if icon_create_function else '' # noqa
options = {} if options is None else options
if icon_create_function is not None:
options['iconCreateFunction'] = icon_create_function.strip()
self.options = json.dumps(options, sort_keys=True, indent=2)

@staticmethod
def _validate(obj, cls):
"""Check whether the given object is from the given class or is None."""
return True if obj is None or isinstance(obj, cls) else False

def render(self, **kwargs):
super(MarkerCluster, self).render(**kwargs)
Expand Down