[GIS] Folium and MarkerCluster

foliumipythonleaflet

I'm trying to use Folium but am having difficulty adding multiple points to a MarkerCluster in one fell swoop. Documentation is… sparse, so I'm not sure how to proceed.

This recreates the problem in an IPython notebook:

import folium print folium.__version__
# __version__ == 0.2.1

Joburg=[-26.2041, 28.0473]
locations=[[-26.15048, 28.10404],  [-28.67343, 32.1051],  [-25.97249, 28.12367]]

# Adding the markers one-by-one works fine:
map1 = folium.Map(location=Joburg,zoom_start=9)
my_marker_cluster1 = folium.MarkerCluster()
for location in locations:
    folium.Marker(location=location).add_to(my_marker_cluster1)
map1.add_children(my_marker_cluster1)

# Adding them together fails (i.e., the map renders, but with no markers on it)
map2 = folium.Map(location=Joburg,zoom_start=9)
my_marker_cluster2 = folium.MarkerCluster(locations)
map2.add_children(my_marker_cluster2)

Best Answer

In your first case, you're adding Marker objects iteratively to your MarkerCluster. In the second case, you are just giving a list of coordinates, but not Marker objects. Convert your locations list first:

locations = [folium.Marker(l) for l in locations]

This is incorrect advice, see comment and the MarkerCluster source.