3

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)
3

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.

  • Thank you, but this doesn't seem to work for me. (Same problem-- markers don't render.) Moreover, if I use heatmap_map.add_children(plugins.HeatMap(locations)) (where locations is a list of coordinates rather than Marker objects), it does work. (But maybe the API is just inconsistent?) – Bill Bradley Apr 25 '16 at 20:03
  • Actually, I was only assuming that that was your problem, without checking the Folium API. Indeed, your example should work, as MarkerCluster says that it takes a list of a list or array of shape (n,2), e.g. [[lat,lon]]. You probably have some other problem. – Richard Law Apr 25 '16 at 23:04
  • If you run the sample code, does it work for you? That would help me understand where the "other problem" might be. – Bill Bradley Apr 28 '16 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.