Location Marking with Folium in Python

Arturas Vycas
Geek Culture
Published in
4 min readSep 9, 2021
Photo by Timo Wielink on Unsplash

Did you ever need to add markers on the map to visualize some data? Don’t worry about that anymore. It turns out that it is quite easy to add location markers on the map with Python using Folium library. Official documentation for Folium library can be found here.

For this tutorial, I will assume that you already have Python installed. So the first thing you need to do is install Folium library. You can do that by executing the following command:

pip install folium

And that’s pretty much it. You are ready to visualize markers on the map. So next thing to do is obviously to import Folium to our code.

import folium

So to start working with the library, you need to create Folium map object.

folium_map = folium.Map()

The next thing to do is to get coordinates which you want to show on the map. For the purpose of only demonstrating the principle, I have selected a few popular places to visit in Italy, Rome. Coordinates are provided below:

# Latitude, Longitude
LOCATION_DATA = [
("41.90093256", "12.48331626"),
("41.89018285", "12.49235900"),
("41.89868519", "12.47684474"),
("41.89454167", "12.48303163"),
("41.90226256", "12.45739340"),
("41.90269661", "12.46635787"),
("41.91071023", "12.47635640"),
("41.90266442", "12.49624457")
]

The next thing to do is to add markers on the map with these locations. The easiest way to do is by loop through LOCATION_DATA list and add those to folium_map object.

for cords in LOCATION_DATA:
folium.Marker(location=[cords[0], cords[1]]).add_to(folium_map)

The last step is to save this information into html file which can be opened by your browser.

folium_map.save("FoliumMap.html")

Now if you put everything up together correctly, your output should be “FoliumMap.html” file. Of course, if you open this file, you would see a similar view to the one provided below.

Image 1 — View after opening “FoliumMap.html” file in your browser (image by Author)

Of course, you can zoom the view, but generally you want to see the view in zoom from the beginning. You can actually do that when you are creating Folium map object. For that, you need provide the location and zoom_start parameters. In this case, I selected Location as the first point from LOCATION_DATA list and zoom_start=14 as this gives quite a good view on my laptop screen size.

folium_map = folium.Map(location=[LOCATION_DATA[0][0], LOCATION_DATA[0][1]], zoom_start=14)

If we run the same code now, the output would look something like this:

Image 2 — View after opening “FoliumMap.html” file in your browser when providing initial location and zoom_start parameter (image by Author)

This method is good enough if you know where your marks should be on the map. However, it would be better to zoom into location where marks are automatically.

Auto zoom

It turns out, that Folium has a method which helps to auto zoom into area where our markers are located. That method is called fit_bounds. You can read more about it here.

According to documentation, we need to provide southwest and northeast corners in form of a list made of latitude and longitude points. Having LOCATION_DATA in our format, we can get these points quite easily:

south_west_corner = min(LOCATION_DATA)
north_east_corner = max(LOCATION_DATA)

The only thing that is left is to call fit_bounds method on our folium_map object:

folium_map.fit_bounds([south_west_corner, north_east_corner])

This gives us result shown below:

Image 3 — View after opening “FoliumMap.html” file in your browser after applying fit_bounds method (image by Author)

Add popup to Marker

Of course, putting bunch of markers on the map may mean something to you, but not to others, so that’s why we should add some information to our markers — let’s say latitude, longitude and location name. For that I will add a new list which location names according to coordinates.

LOCATION_NAMES = [
"Trevi Fountain",
"Colosseum",
"Pantheon",
"Piazza Venezia",
"St. Peter’s Square",
"Mausoleum of Hadrian",
"Piazza del Popolo",
"Fountain of the Naiads"
]

Now the only thing left to do is add this information to our marker:

for cords, name in zip(LOCATION_DATA, LOCATION_NAMES):
folium.Marker(location=[cords[0], cords[1]],
popup=f"Lattitude:<br>{cords[0]}<br>"
f"Longitude:<br>{cords[1]}<br>"
f"Name:<br>{name}"
).add_to(folium_map)

Now you can click on markers and see additional information

Image 4 — View after opening “FoliumMap.html” file in your browser after adding popup information to markers (image by Author)

One thing to note here is that text in popup has to be in html format. Also, you will probably notice that text is not shown nicely in the popup. It can be solved by using customized popups, but I will leave that for another time. If you want to search how to do this on your own, you should look for folium.Popup class. Documentation for that can be found here.

Conclusion

As you can see, working with Folium library is quite easy and you can visualize data on the map in no time. Of course this shows just the basics of what you can do with Folium

I have uploaded final code version to my github repository: https://github.com/vycart/folium_markers

If you have any questions, it will be my pleasure to answer them.

--

--

Arturas Vycas
Geek Culture

Embedded Software Engineer, Python enthusiast. I love to share knowledge and learn new things.