Home > Software engineering >  How to add popup in a folium map matching a loop format?
How to add popup in a folium map matching a loop format?

Time:10-04

I have a folium map code (python) working in a loop mode, the following code works well except that the popup insert does not work after many tries (give same information for all point or like in current code formulation error from the popup part)

(database columns : "Latitude", "Longitude", "SA", "cluster-db" )

I think that the index is not correct and would like to know which one could works in that precise case.

import matplotlib.cm as cm
import matplotlib.colors as colors

map_clusters = folium.Map(location=[data['Latitude'].mean(), data_['Longitude'].mean()],
               tiles='CartoDB positron', zoom_start=7)

# set color scheme for the clusters
x = cluster
ys = [i   x   (i*x)**2 for i in range(len(x))]
colors_array = cm.rainbow(np.linspace(0, 1, len(ys)))
rainbow = [colors.rgb2hex(i) for i in colors_array]

# add markers to the map
markers_colors = []
for lat, lng, cluster in zip(data['Latitude'], data['Longitude'],  
                                            data['cluster-db']):

    folium.vector_layers.CircleMarker(
        [lat, lng],
        radius=5,
        #tooltip =  '- Cluster '   str(cluster),
        popup = (data.iloc[lat]['Sa'], data.iloc[lat]['Latitude'], dat.iloc[lat]['Longitude']),
        color=rainbow[cluster-1],
        fill=True,
        fill_color=rainbow[cluster-1],
        fill_opacity=0.9).add_to(map_clusters)
       
map_clusters

CodePudding user response:

I think these iloc-indexing here is perhaps wrong? popup = (data.iloc[lat]['Sa'], data.iloc[lat]['Latitude'], dat.iloc[lat]['Longitude'])

Your variables lat, lng, cluster from for loop for lat, lng, cluster in zip(data['Latitude'], data['Longitude'], data['cluster-db']): are values from the columns but you tried to use them as index in the iloc. Or as you need the values just use them?

popup = (lat, lng) (plus the values of column SA, you have to define that in your for .. zip(..

Note: At the first run of your script the value cluster in x = cluster is not defined

  • Related