Introduction:
Python is a compelling language and also very rich in libraries. phonenumbers is one of the modules that provides numerous features like providing basic information about a phone number, validation of a phone number, etc. Here, we will learn how to use phonenumbers module along with folium and opencage packages to get the location of a phone number on a Map.
Project Prerequisites:
You have to install the following packages for this project :
Build your own chatGPT-3 Using Python
pip install folium opencage phonenumbers
# phonenumbers
# folium
# opencage
- To know more about the phonenumbers package can read this documentation – https://pypi.org/project/phonenumbers/
- To We also need to get the API Key from the opencagedata.com
- All you need to do is signup and then go to the Geocoding API tab and copy the API Key and save it in notepad. click here

Getting Started:
NOTE: I will be using PyCharm Community Edition IDE. So, the steps I mention are specific to that IDE only.
You can also write the program in the IDE of your choice; I have mentioned the requirements in the end notes of this article.
First of all, we need to create a project folder named – “TrackPhoneNumberMap” and create a python file named phoneNumberTrack.py
Installing the required package:
· Click on the file button and then Settings, or you can directly press Ctrl+Alt+S
to open the settings
· There, you will find the Project: TrackPhoneNumberMap option. Click on that and then on the Python Interpreter.
· Now, to install the folium package, click on the ‘+’ symbol above the Package and search for folium and then click Install Package.

· Now open the terminal and put in the command pip install phonenumbers the phonenumbers package will be installed.

· After that, use the command pip install opencage to install the opencage package.

Code Implementation:
First, we import the phonenumbers package.
We will import geocoder and carrier modules to get the geological location and the service provider name, respectively.
import phonenumbers
from phonenumbers import geocoder
from phonenumbers import carrier
import phonenumbers
from phonenumbers import geocoder
from phonenumbers import carrier
We will also import folium and form opencage package we import OpenCageGeocode Module.
import folium
from opencage.geocoder import OpenCageGeocode
import folium
from opencage.geocoder import OpenCageGeocode
After that, we will take input the phone number that is to be tracked. The phone number has to be entered along with the country code.
number = input(“Enter the phone number with country code : “)
number = input("Enter the phone number with country code : ")
Now that we have taken the input of the phone number in the form of a string, we need to convert it into phone number format, and this is done using the parse( ) method of phonenumber module.
#parsing String to the Phone number
phoneNumber = phonenumbers.parse(number)
# Parsing String to the Phone number
phoneNumber = phonenumbers.parse(number)
We will store the API Key that we saved in the Notepad into the Key variable.
Key = “e262d8a9fc864d4c85f5e7639c58c227” #example api
Key = "e262d8a9fc864d4c85f5e7639c58c227" #example api
After that, we will be using the phoneNumber to print the geo-location and service provider name of the entered phone number.
First, we will print the geo location of the phone number using the description_for_number( ) method from the geocoder module that is imported from the phonenumbers package.
# printing the geolocation of the given number using the geocoder module
geolocation = geocoder.description_for_number(phoneNumber,”en”)
print(“location : “+geolocation)
# printing the geolocation of the given number using the geocoder module
geolocation = geocoder.description_for_number(phoneNumber,"en")
print("location : "+geolocation)
Second, we print the carrier/service provider name with the help of the name_for_number( ) method from the carrier module.
# printing the service provider name using the carrier module
service = carrier.name_for_number(phoneNumber,”en”)
print(“service provider : “+service)
# printing the service provider name using the carrier module
service = carrier.name_for_number(phoneNumber,"en")
print("service provider : "+service)
After, we extract the complete location using the API key with the opencage methods
using opencage to get the latitude and longitude of the location
geocoder = OpenCageGeocode(Key)
query = str(yourLocation)
results = geocoder.geocode(query)
# Using opencage to get the latitude and longitude of the location
geocoder = OpenCageGeocode(Key)
query = str(yourLocation)
results = geocoder.geocode(query)
Now, we use results to get the latitude and longitude of the location we are tracking
#assigning the latitude and longitude values to the lat and lng variables
lat = results[0][‘geometry’][‘lat’]
lng = results[0][‘geometry’][‘lng’]
# Assigning the latitude and longitude values to the lat and lng variables
lat = results[0]['geometry']['lat']
lng = results[0]['geometry']['lng']
After, we use Map() method from folium to get the map of given latitude and longitude
#Getting the map for the given latitude and longitude
myMap = folium.Map(loction=[lat,lng],zoom_start = 9)
# Getting the map for the given latitude and longitude
myMap = folium.Map(loction=[lat,lng],zoom_start = 9)
We also use the Marker() method from folium to mark the location with the location name on the map.
#adding a Marker on the map to show the location name
folium.Marker([lat,lng],popup=yourLocation).add_to(myMap)
# Adding a Marker on the map to show the location name
folium.Marker([lat,lng],popup=yourLocation).add_to(myMap)
Now for the final step, we save the map in HTML file format that can be later viewed in a browser.
The file will be created in the same file as the project with the name that we have given in the code(Location.html)
#save the map to an HTML file to open it and see the actual location in map format
myMap.save(“Location.html”)
Source Code:
Here is the complete source code:
# track location with the map using the phone number
import phonenumbers
from phonenumbers import geocoder
from phonenumbers import carrier
import folium
from opencage.geocoder import OpenCageGeocode
# taking input the phonenumber along with the country code
number = input("Enter the PhoneNumber with the country code : ")
# Parsing the phonenumber string to convert it into phonenumber format
phoneNumber = phonenumbers.parse(number)
# Storing the API Key in the Key variable
Key = " Enter your Api" #generate your api https://opencagedata.com/api
# Using the geocoder module of phonenumbers to print the Location in console
yourLocation = geocoder.description_for_number(phoneNumber,"en")
print("location : "+yourLocation)
# Using the carrier module of phonenumbers to print the service provider name in console
yourServiceProvider = carrier.name_for_number(phoneNumber,"en")
print("service provider : "+yourServiceProvider)
# Using opencage to get the latitude and longitude of the location
geocoder = OpenCageGeocode(Key)
query = str(yourLocation)
results = geocoder.geocode(query)
# Assigning the latitude and longitude values to the lat and lng variables
lat = results[0]['geometry']['lat']
lng = results[0]['geometry']['lng']
# Getting the map for the given latitude and longitude
myMap = folium.Map(loction=[lat,lng],zoom_start = 9)
# Adding a Marker on the map to show the location name
folium.Marker([lat,lng],popup=yourLocation).add_to(myMap)
# save map to html file to open it and see the actual location in map format
myMap.save("Location.html")
Woo-hoo! That’s all for the code.
NOTE: This program can be executed in any IDE of your choice. You only need to install the phonenumbers, folium, and opencage packages using the cmd.
Command for installing phonenumbers through cmd – pip install phonenumbers
folium through cmd – pip install folium
opencage through cmd – pip install opencage
Now let’s run and see the output. The output should look something like this:
First, we get to enter the phone number we want to locate along with its country code.

After that, the location and Carrier name will be displayed and an HTML file will be created with the given name.


Now, you can open the HTML file in the browser of your choice and see the location on Map.


Congratulations now you have learned how to find the location of a phone number with the Map.
api key has been blocked
please generate a new api key from https://developers.google.com/maps/documentation/geocoding/overview