Search code examples
pythonfolium

Folium - How to add all IDs with same coordinates to pup up, or make stacked markers visible?


So I have a folium map built using the following code.

m = folium.Map(location=[40, -90], zoom_start=4)
colors = ['gray','green','blue']

# add marker one by one on the map
for i in range(0,len(df)):
    folium.Marker(
        location=[df.iloc[i]['LAT'], df.iloc[i]['LNG']],
        popup = folium.Popup(folium.IFrame('Plant: ' + str((df.iloc[i]['ID'])) + 
                                           '<br>' + 'Name: ' + (df.iloc[i]['name']) + 
                                           '<br>'+ 'Address: ' + (df.iloc[i]['address'])), 
                             min_width=300, max_width=300),
        icon=folium.Icon(color=colors[df.iloc[i].group])
    ).add_to(m)
map

The problem is that there are about 15 IDs that have the exact same coordinates. I want to either show all IDs with those coordinates in the same pop up or somehow adjust the markers so that all of them at that location as visible. Is this possible with Folium? An example of my df is below.

ID  Name                    Address     LAT         LNG
1   The Interactive College 1 Street    28.38535861 102.6589614
2   Bloom Marketing         2 Street    41.39434884 85.67897377
3   Hearty Pancake          3 Street    47.3863284  100.7854692
4   The Auto DNA            4 Street    45.39532869 69.55646099
5   Urban Philosophy        5 Street    37.38631845 81.72556255
1   Raven                   6 Street    28.38535861 102.6589614
2   Gourmet Sandwich        7 Street    41.39434884 85.67897377
3   Office Tile             8 Street    47.3863284  100.7854692
4   The Crunchy Croissant   9 Street    45.39532869 69.55646099
5   Smart Phone Repair      10 Street   37.38631845 81.72556255

Solution

  • This answer is based on the assumption that all overlapping latitude and longitude in the data presented in the question are in two locations. Group the existing data frame by latitude and longitude and list the other columns column by column. The marker process will create a pop-up for each row unit of that grouped data frame. And since the data presented did not have a column called Group, we used the ID column as the color coding. Please modify this to fit your data. If there are three or more locations with the same latitude and longitude, get the length of the list in the ID column and combine ID,Name,Address in a loop process. It would be easier to display multiple information in a popup rather than shifting the overlap by latitude and longitude.

    dfg = df.groupby(['LAT','LNG']).agg(lambda x: list(x)).reset_index()
    
    dfg
    
        LAT     LNG     ID  Name    Address
    0   28.385359   102.658961  [1, 1]  [The Interactive College, Raven]    [1 Street, 6 Street]
    1   37.386318   81.725563   [5, 5]  [Urban Philosophy, Smart Phone Repair]  [5 Street, 10 Street]
    2   41.394349   85.678974   [2, 2]  [Bloom Marketing, Gourmet Sandwich]     [2 Street, 7 Street]
    3   45.395329   69.556461   [4, 4]  [The Auto DNA, The Crunchy Croissant]   [4 Street, 9 Street]
    4   47.386328   100.785469  [3, 3]  [Hearty Pancake, Office Tile]   [3 Street, 8 Street]
    
    import folium
    
    m = folium.Map(location=[df.LAT.mean(), df.LNG.mean()], zoom_start=4)
    colors = ['gray','green','blue','red','orange']
    
    for row in dfg.itertuples():
        folium.Marker(
            location=[row.LAT, row.LNG],
            popup = folium.Popup(folium.IFrame('Plant: ' + str(row.ID[0]) + ', ' + str(row.ID[1]) + 
                                               '<br>' + 'Name: ' + row.Name[0] + ', ' + row.Name[1] + 
                                               '<br>'+ 'Address: ' + row.Address[0]  + ', ' + row.Address[1]), min_width=250, max_width=600),
            icon=folium.Icon(color=colors[row.Index])
        ).add_to(m)
    m
    

    enter image description here