Learn Python – How to Plot the Google Map using folium package in Python- Basic and advance

The folium package deal is constructed on the records wrangling strengths of the Python ecosystem and the mapping strengths of the Leaflet.js library of JavaScript language. The user can manipulate their data by means of the usage of Python and then visualize it via using Leaflet.js map thru folium package. Folium bundle is an handy method of visualizing the information on Leaflet.js map, which has been manipulated through the use of Python.

Required Module and Libraries

Folium: The person can deploy the Folium package with the aid of the use of the following command.

pip install folium  

Geopy: The geopy module of Python makes it effortless for Python users to detect the coordinates of landmarks, cities, countries on the earth’s surface. For putting in the geopy module, the consumer can use the following command:

pip install geopy  

After successful set up of the both libraries, we observe the below steps to plot Google map.

Step 1: Create the Base map

The consumer can create the base map by way of the use of the following program:

import os  
# First, import folium package  
import folium  
from geopy.geocoders import Nominatim as NT  
# Initialize Nominatim API  
geo_locator = NT(user_agent = "geoapiExercises")  
# write the place  
place_1 = "Yemen"  
  
location_1 = geo_locator.geocode(place_1)  
# now, it will search for the location by using the latitude and longitude, with zoom_start = 15  
user_map1 = folium.Map(location = [location_1.longitude, location_1.latitude],  
                                        zoom_start = 15 )  
# At last, open the base map  
user_map1  

Output:

Step 2: Add a Circular Marker

The consumer can mark the location with the circle and popup textual content by using using the following code:

import folium  
   
from geopy.geocoders import Nominatim as NT  
  
geo_locator = NT(user_agent = "geoapiExercises")  
  
place_1 = "Yemen"  
  
location_1 = geo_locator.geocode(place_1)  
  
user_map1 = folium.Map(location = [location_1.longitude, location_1.latitude],  
                                        zoom_start = 15 )  
   
# CircleMarker with radius  
folium.CircleMarker(location = [location_1.longitude, location_1.latitude],  
                    radius = 45, popup = ' YEMEN ').add_to(user_map1)  
   
# Now, open the Map with circular Mark  
user_map1  

Output:

Step 3: Add the easy marker for the parachute fashion marker with the popup textual content

The user can use the following code.

Example –

import os  
import folium  
   
from geopy.geocoders import Nominatim as NT  
  
geo_locator = NT(user_agent = "geoapiExercises")  
  
place_1 = "Yemen"  
  
location_1 = geo_locator.geocode(place_1)  
  
user_map1 = folium.Map(location = [location_1.longitude, location_1.latitude],  
                                        zoom_start = 15)  
#Now, we will pass the string in popup parameter  
folium.Marker([location_1.longitude, location_1.latitude],  
               popup = ['YEMEN']).add_to(user_map1)  
# now, open the map   
user_map1  

Output:

Step 4: Add the line on the map

The consumer can use the following code for including the line on the map to be part of the two coordinates.

Example –

# First, import folium package  
import folium  
import os   
from geopy.geocoders import Nominatim as NT  
  
geo_locator = NT(user_agent = "geoapiExercises")  
  
place_1 = "Aden"  
place_2 = "Yemen"  
location_1 = geo_locator.geocode(place_1)  
location_2 = geo_locator.geocode(place_2)  
user_map1 = folium.Map(location = [location_1.longitude, location_1.latitude],  
                                        zoom_start = 6)  
   
folium.Marker([location_1.longitude, location_1.latitude],  
               popup = ['Aden']).add_to(user_map1)  
   
folium.Marker([location_2.longitude, location_2.latitude],  
              popup = 'Yemen').add_to(user_map1)  
   
# Now, we will add the line on the map by using Polyline method .  
# it will connect both coordinates by the line  
   
folium.PolyLine(locations = [[location_1.longitude, location_1.latitude], [location_2.longitude, location_2.latitude]],  
                line_opacity = 0.5).add_to(user_map1)  
# now, open the map  
user_map1  

Output:

Explanation

We used the geopy library to get the latitude and longitude of the location. Then we used the “folium.map” method of the folium bundle for growing the base of Google Maps.

In step 2, we used “folium.CircleMarker” for marking the round mark on the area with the pop-up text. In step 3, we used “folium.Marker” to add a parachute style mark on the cited location. In the ultimate step, we used “folium.PolyLine” for joining two marks on two one of a kind places on the map.

Conclusion

In this tutorial, we have shown how the user can Plot the Google map and add special required functionalities on the map like a round mark, parachute mark, pop-up text, and the line becoming a member of the two coordinates on the map.