Python Folium Google Maps Tutorial

The folium library leverages the data manipulation capabilities of the Python ecosystem in conjunction with the mapping prowess of the Leaflet.js JavaScript library. Users can process their data utilizing Python and subsequently represent it visually on a Leaflet.js map via the folium library. This package offers a straightforward method to visualize data on a Leaflet.js map that has been processed through Python.

Required Module and Libraries

Folium:

To install the Folium package, the user can execute the command provided below.

Example

pip install folium

Geopy:

The geopy library in Python simplifies the process for developers to find the geographical coordinates of various locations such as landmarks, cities, and countries on the planet. To install the geopy library, users can execute the command below:

Example

pip install geopy

Upon the successful installation of both libraries, we proceed with the following steps to create a plot on Google Maps.

Step for Plotting the Google Map using Folium package

The subsequent steps outline the procedure for creating a Google Map utilizing the Folium library in Python:

Step 1: Create the Base map

The foundational map can be generated by utilizing the subsequent program:

Example

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:

Image 1

Step 2: Add a Circular Marker

The user has the ability to highlight a specific region with a circle and accompanying popup text by utilizing the subsequent code:

Example

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:

Image 2

Step 3: Add the simple marker for the parachute style marker with the popup text

The user can use the following code.

Example:

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:

Image 3

Step 4: Add the line on the map

The individual can implement the subsequent code to create a line on the map that connects the two specified coordinates.

Example:

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:

Image 4

Explanation

We utilized the geopy library to obtain the latitude and longitude coordinates of the specified location. Subsequently, we employed the "folium.map" function from the folium package to establish the foundational layout of Google Maps.

During step 2, we utilized "folium.CircleMarker" to denote the specified location with a circular marker accompanied by a pop-up message. In step 3, we employed "folium.Marker" to place a parachute-style marker at the designated location. Finally, in the concluding step, we implemented "folium.PolyLine" to connect two markers situated at distinct locations on the map.

Conclusion

In this instructional guide, we have demonstrated how users can render a Google map and incorporate various essential features onto the map, such as a circular marker, a parachute marker, pop-up text, and a line connecting two specified coordinates on the map.

Input Required

This code uses input(). Please provide values below: