Draw maps using latitude and longitude using folium in Python

Hola CodeSpeeders! In this tutorial, we will learn how to draw maps efficiently using latitude and longitude in Python using the folium library along with the geographical data.

Draw maps using latitude and longitude

First, we need to install Folium Library:

pip install folium

We will import the folium library:

import folium

We will create a list named locations which has Latitude and Longitude coordinates and labels of locations we need. I have taken Hyderabad, Chennai, and Bengaluru:

locations = [
(17.385044, 78.486671, 'Hyderabad'),
(13.082680, 80.270721, 'Chennai'),
(12.971598, 77.594562, 'Bengaluru'),
]

We will create a folium map object centered around the specified coordinates which are the center of India with an initial zoom level of 5. This helps us in  adding markers and other elements to the map:

map_center = [20.5937, 78.9629] #Center coordinates of India 
my_map = folium.Map(location=map_center, zoom_start=5)

We will add markers to the folium map for each specified latitude, longitude, and label. The markers include popups with formatted text displaying the name of location and corresponding geographic coordinates.

for lat, lon, label in locations:
    popup_text = f"<b>{label}</b><br>Latitude: {lat}<br>Longitude: {lon}"
    folium.Marker([lat, lon], popup=popup_text).add_to(my_map)

We will save the map to HTML file:

my_map.save("india_cities_map.html")

Finally, we will display the map using:

my_map

Output:
draw maps using latitude and longitude using folium in Python

“Happy Learning, Keep Growing!”

Also learn: Calculating the distance between two places using geopy Python

Leave a Reply

Your email address will not be published. Required fields are marked *