Search code examples
pythonpython-3.xmapsfolium

Can we add annotations, or some kind of labels, to Folium Maps?


I have a basic SQL script which pulls data from MySQL , adds it to a dataframe, and then a Folium Map is created.

Here is my code.

#### login to DB
#### df = pd.read_sql("SELECT ... FROM ...)

m = folium.Map(location=[40.6976701, -74.2598704], zoom_start=10)

locations = list(zip(df.Latitude, df.Longitude))
#print(locations)

cluster = MarkerCluster(locations=locations)
m.add_child(cluster)

m

That produces this awesome map.

enter image description here

I can zoom in or zoom out, and the clusters expand or combine dynamically. Clearly the numbers are counts of items per cluster. I am wondering if I can add in another data point, like summing expenses per counts of items. So, in the image here, we can see a 3 at the top center. If that consists of 3 seperate expenses of 200 each, can I show the 600 as some kind of annotation, or label, pointing to the cluster? In the documentation I saw a parameters called popup and tooltip, but it doesn't seem to work for me.

Maybe I need to do some kind of aggregation, like this.

df.groupby(['Latitude','Longitude']).sum()

Just thinking out loud here.


Solution

  • I ended up doing this.

    m = folium.Map(location=[40.6976701, -74.2598704], zoom_start=10)
    for lat,lon,name,tip in zip(df.Latitude, df.Longitude, df.SiteName, df.Site):
        folium.Marker(location=[lat,lon], tooltip = tip, popup = name)
    m.add_child(cluster)
    m
    

    This lets you add a tooltip and a popup. That's pretty helpful. I still can't find a way to do sums. It seems like the only option is counts.