Folium is a Python package that combines the data manipulation capabilities of Python with the mapping capabilities of the Leaflet.js library in JavaScript. With Folium, users can manipulate their data using Python and then easily visualize it on a Leaflet.js map. This integration allows for a seamless workflow, where data can be processed and analyzed in Python, and then displayed on an interactive map for visualization. Folium simplifies the process of creating interactive maps with Python, making it accessible to users with varying levels of programming experience.
How To Install the package?
pip install folium
Geopy – Now, since we want to plot the google map, we will be using the package which is geopy in python, which will help to generate coordinates. We can install it with the below line of code –
pip install geopy
Now, that we have both the packages installed let us create google map plot
Step 1: First let us create a base map
import folium
from geopy.geocoders import Nominatim
from folium.vector_layers import CircleMarker
# Create a base map
geolocator = Nominatim(user_agent="CityExplorerMapScript")
location = geolocator.geocode("Jamshedpur")
map = folium.Map(location=[location.latitude, location.longitude], zoom_start=10)
# Display the map
map.save('map.html')
Output:
Explanation of the above code:
import folium: Imports the Folium library, which is used for creating interactive maps in Python.from geopy.geocoders import Nominatim: Imports the Nominatim geocoder from the Geopy library, which is used for converting addresses into latitude and longitude coordinates.from folium.vector_layers import CircleMarker: Imports the CircleMarker class from the Folium library, which is used to add circular markers to the map.geolocator = Nominatim(user_agent="CityExplorerMapScript"): Creates a Nominatim geolocator object with a custom user agent string “CityExplorerMapScript”. This user agent string is used to identify the application making the geocoding requests.location = geolocator.geocode("Jamshedpur"): Uses the geolocator object to geocode the location “Jamshedpur” into latitude and longitude coordinates.map = folium.Map(location=[location.latitude, location.longitude], zoom_start=10): Creates a new Folium map centered at the latitude and longitude coordinates of “Jamshedpur” with a zoom level of 10.CircleMarker( location=[location.latitude, location.longitude], radius=50, color='red', fill=True, fill_color='red' ).add_to(map): Creates a circular marker at the latitude and longitude coordinates of “Jamshedpur” with a radius of 50 pixels, red border color, and red fill color, and adds it to the map.map.save('map.html'): Saves the map as an HTML file named “map.html” in the current directory. This HTML file can be opened in a web browser to view the map.
Step 2- Let us create the circular mark.
# Add a circular marker
CircleMarker(
location=[location.latitude, location.longitude],
radius=50,
color='red',
fill=True,
fill_color='red'
).add_to(map)
Full Code:
import folium
from geopy.geocoders import Nominatim
from folium.vector_layers import CircleMarker
# Create a base map
geolocator = Nominatim(user_agent="CityExplorerMapScript")
location = geolocator.geocode("London")
map = folium.Map(location=[location.latitude, location.longitude], zoom_start=10)
# Add a circular marker
CircleMarker(
location=[location.latitude, location.longitude],
radius=50,
color='red',
fill=True,
fill_color='red'
).add_to(map)
# Display the map
map.save('map.html')

Step 3: How To Add the simple marker for the parachute style marker with the popup text
import folium
from geopy.geocoders import Nominatim
from folium.vector_layers import CircleMarker
# Create a base map
geolocator = Nominatim(user_agent="CityExplorerMapScript")
location = geolocator.geocode("London")
map = folium.Map(location=[location.latitude, location.longitude], zoom_start=10)
# Add a circular marker
CircleMarker(
location=[location.latitude, location.longitude],
radius=50,
color='red',
fill=True,
fill_color='red'
).add_to(map)
# Add a simple marker with the parachute icon and a popup text
folium.Marker(
location=[location.latitude, location.longitude],
icon=folium.Icon(icon='parachute', prefix='fa'),
popup='London, the capital of the United Kingdom'
).add_to(map)
# Display the map
map.save('map.html')
Output:

Step 4: Add a line on the map
import folium
from geopy.geocoders import Nominatim
from folium.vector_layers import CircleMarker
# Create a base map
geolocator = Nominatim(user_agent="CityExplorerMapScript")
location_london = geolocator.geocode("London")
location_paris = geolocator.geocode("Paris")
map = folium.Map(location=[location_london.latitude, location_london.longitude], zoom_start=5)
# Add a circular marker for London
CircleMarker(
location=[location_london.latitude, location_london.longitude],
radius=50,
color='red',
fill=True,
fill_color='red'
).add_to(map)
# Add a circular marker for Paris
CircleMarker(
location=[location_paris.latitude, location_paris.longitude],
radius=50,
color='blue',
fill=True,
fill_color='blue'
).add_to(map)
# Add a simple marker with the parachute icon and a popup text for London
folium.Marker(
location=[location_london.latitude, location_london.longitude],
icon=folium.Icon(icon='parachute', prefix='fa'),
popup='London, the capital of the United Kingdom'
).add_to(map)
# Add a simple marker with the parachute icon and a popup text for Paris
folium.Marker(
location=[location_paris.latitude, location_paris.longitude],
icon=folium.Icon(icon='parachute', prefix='fa'),
popup='Paris, the capital of France'
).add_to(map)
# Add a line connecting London and Paris
folium.PolyLine(
locations=[[location_london.latitude, location_london.longitude],
[location_paris.latitude, location_paris.longitude]],
color='green',
weight=2.5,
opacity=1
).add_to(map)
# Display the map
map.save('map.html')
Output:

Explanation of the above code:
import folium: Imports the Folium library, which is used for creating interactive maps in Python.from geopy.geocoders import Nominatim: Imports the Nominatim geocoder from the Geopy library, which is used for converting addresses into latitude and longitude coordinates.from folium.vector_layers import CircleMarker: Imports the CircleMarker class from the Folium library, which is used to add circular markers to the map.geolocator = Nominatim(user_agent="CityExplorerMapScript"): Creates a Nominatim geolocator object with a custom user agent string “CityExplorerMapScript”. This user agent string is used to identify the application making the geocoding requests.location_london = geolocator.geocode("London"): Uses the geolocator object to geocode the location “London” into latitude and longitude coordinates.location_paris = geolocator.geocode("Paris"): Uses the geolocator object to geocode the location “Paris” into latitude and longitude coordinates.map = folium.Map(location=[location_london.latitude, location_london.longitude], zoom_start=5): Creates a new Folium map centered at the latitude and longitude coordinates of London with a zoom level of 5.CircleMarker(location=[location_london.latitude, location_london.longitude], radius=50, color='red', fill=True, fill_color='red').add_to(map): Adds a circular marker for London with a radius of 50 pixels, red border color, and red fill color to the map.CircleMarker(location=[location_paris.latitude, location_paris.longitude], radius=50, color='blue', fill=True, fill_color='blue').add_to(map): Adds a circular marker for Paris with a radius of 50 pixels, blue border color, and blue fill color to the map.folium.Marker(location=[location_london.latitude, location_london.longitude], icon=folium.Icon(icon='parachute', prefix='fa'), popup='London, the capital of the United Kingdom').add_to(map): Adds a simple marker with the parachute icon and a popup text “London, the capital of the United Kingdom” for London to the map.folium.Marker(location=[location_paris.latitude, location_paris.longitude], icon=folium.Icon(icon='parachute', prefix='fa'), popup='Paris, the capital of France').add_to(map): Adds a simple marker with the parachute icon and a popup text “Paris, the capital of France” for Paris to the map.folium.PolyLine(locations=[[location_london.latitude, location_london.longitude], [location_paris.latitude, location_paris.longitude]], color='green', weight=2.5, opacity=1).add_to(map): Adds a line connecting London and Paris to the map with a green color, a weight of 2.5 pixels, and full opacity.map.save('map.html'): Saves the map as an HTML file named “map.html” in the current directory. This HTML file can be opened in a web browser to view the map.
We used the geopy library to get the latitude and longitude of the location. Then we used the “folium.map” method of the folium package for creating the base of Google Maps.
In step 2, we used “folium.CircleMarker” for marking the circular mark on the location with the pop-up text. In step 3, we used “folium.Marker” to add a parachute style mark on the mentioned location. In the last step, we used “folium.PolyLine” for joining two marks on two different locations on the map.
In this tutorial, we learned how to use the Folium package in Python to plot Google Maps. Folium is a powerful library that allows us to create interactive maps with various functionalities. Here’s a summary of what we covered:
- Importing Libraries: We imported the necessary libraries, including Folium for map plotting and Geopy for geocoding.
- Creating a Base Map: We created a base map using Folium, specifying the location and zoom level.
- Adding Markers: We learned how to add markers to the map, including simple markers and markers with custom icons. We also added popup text to the markers.
- Adding a Line: We added a line between two locations on the map using the
PolyLineclass. - Customizing Markers and Lines: We explored various customization options for markers and lines, such as color, size, and opacity.
- Saving the Map: Finally, we saved the map as an HTML file that can be opened in a web browser to view the interactive map.
By following this tutorial, you should now have a good understanding of how to plot Google Maps using the Folium package in Python. This can be particularly useful for visualizing geographical data or creating interactive maps for web applications.





Leave a Reply