#312 Add markers on folium map

This page describes how to add markers to your folium map. It is done using the folium.Marker function. Note that you can custom the popup window of each marker with any html code!


# import libraries
import folium
import pandas as pd

# Make a data frame with dots to show on the map
data = pd.DataFrame({
'lat':[-58, 2, 145, 30.32, -4.03, -73.57, 36.82, -38.5],
'lon':[-34, 49, -38, 59.93, 5.33, 45.52, -1.29, -12.97],
'name':['Buenos Aires', 'Paris', 'melbourne', 'St Petersbourg', 'Abidjan', 'Montreal', 'Nairobi', 'Salvador']
})
data

# Make an empty map
m = folium.Map(location=[20, 0], tiles="Mapbox Bright", zoom_start=2)

# I can add marker one by one on the map
for i in range(0,len(data)):
folium.Marker([data.iloc[i]['lon'], data.iloc[i]['lat']], popup=data.iloc[i]['name']).add_to(m)

# Save it as html
m.save('312_markers_on_folium_map1.html')

  • Sponsors

  • 8 comments

    • pd is not recognized if we don’t import pandas in the code.
      So before importing folium we write this code
      ‘import pandas as pd’
      then code runs perfectly

      Reply
      • The coordinates for the latitude and longitude are switched in the dataframe and switched in the for loop. That is why it probably works but it is all backwards. For example, take Montreal, it should be Latitude: 45.52, Longitude: -73.57. If you pull up the help screen for folium.Marker? (in a Jupyter notebook for example) you can see it is latitude then longitude however other mapping packages do sometimes use longitude then latitude but not folium. See example below for an easy way to see latitude and longitude for any location using folium.

        # use this folium tool to click and see latitude and longitude
        import folium
        m = folium.Map(location=[45, -73], zoom_start=6)
        m.add_child(folium.LatLngPopup())
        m

        It is easy to get latitude and longitude confused because the lines run one way but the measurements run the other way.

        Reply
    • Hi,

      Thanks for this awesome code.

      I trying to plot from a dataframe with around 160 (longitude and latitude) and m will not display

      Is there a limitation to the length of data that can be plotted ?

      Thanks

      Reply
    • if we have 15000 values for latitude and longitude then how to do that
      a all the values are stored inside a csv file
      how to do that with python folium???

      Reply

    Leave a Reply to Nadeem Cancel reply

    Your email address will not be published.