3

I'm trying to create a very simple leaflet/folium map using python.
It works without marker clusters (all the relevant locations show up on the map), but when I try using MarkerCluster I get the following error message 'module' object has no attribute 'MarkerCluster' (in response to line 14, marker_cluster = folium.MarkerCluster().add_to(cyclemap)).
I'm not sure what I'm doing wrong, I've imported the package, I've definitely installed folium correctly.
My text editor also states 'folium.plugins.MarkerCluster imported but unused'.

I'm completely new to python.

import folium
from folium.plugins import MarkerCluster
import pandas as pd
cyclemap = folium.Map(location = [51.5074, -0.1278],
                  tiles='CartoDB positron',
                  zoom_start = 11, 
                  width = 800, height = 600)

cycleincidences = pd.read_csv("2016cyclists.csv")

MAX_RECORDS = 50 
#using max records for speed as I have almost 5000 data points 

marker_cluster = folium.MarkerCluster().add_to(cyclemap)

for each in cycleincidences[0:MAX_RECORDS].iterrows():
 folium.Marker(
    location = [each[1]['X'],each[1]['Y']]
    ).add_to(marker_cluster)

cyclemap.save('cylesmap3.html')

2 Answers 2

3

According to this notebook (https://github.com/python-visualization/folium/blob/master/examples/MarkerCluster.ipynb) you should try calling MarkerCluster directly. This is because you have imported that module already.

marker_cluster = MarkerCluster().add_to(cyclemap)
3

Just to expand a bit on @dslamb's answer, look at the imports in your code.

You use:

from folium.plugins import MarkerCluster

And then in your code you do:

marker_cluster = folium.MarkerCluster().add_to(cyclemap)

which , as your error notes, "doesn't exist".

You need to either use MarkerCluster the same way you imported it, so from the plugins sub-object, like this folium.plugins.MarkerCluster() or directly (since you've imported it as a stand alone too) like MarkerCluster().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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