How to get Geolocation in Python?
Last Updated :
06 Sep, 2022
In this article, we will discuss on how to get Geolocation when you enter any location name and it gives all the useful information such as postal code, city, state, country etc. with the latitudes and the longitudes (the specific coordinates) and vice-versa in which we provide the coordinates to get the location name.
This can be done using the GeoPy library in python. This library isn't built into python and hence needs to be installed explicitly.
Installation
In your terminal, simply run the given command:
pip install geopy
Method 1: Getting coordinates from location name
With provided location, it is possible using geopy to extract the coordinates meaning its latitude and longitude. Therefore, it can be used to express the location in terms of coordinates.
Approach
- Import module
- Import Nominatim from geopy- Nominatim is a free service or tool or can be called an API with no keys that provide you with the data after providing it with name and address and vice versa.
- On calling the Nomination tool which accepts an argument of user_agent you can give any name as it considers it to be the name of the app to which it is providing its services.
- The geocode() function accepts the location name and returns a geodataframe that has all the details and since it's a dataframe we can get the address, latitude and longitude by simply calling it with the given syntax
Syntax:
variablename.address
variablename.latitude
variablename.longitude.
Example:
Python3
# importing geopy library
from geopy.geocoders import Nominatim
# calling the Nominatim tool
loc = Nominatim(user_agent="GetLoc")
# entering the location name
getLoc = loc.geocode("Gosainganj Lucknow")
# printing address
print(getLoc.address)
# printing latitude and longitude
print("Latitude = ", getLoc.latitude, "\n")
print("Longitude = ", getLoc.longitude)
Output:

Method 2: Getting location name from latitude and longitude
In this method all the things are same as the above, the only difference is instead of using the geocode function we will now use the reverse() method which accepts the coordinates (latitude and longitude) as the argument, this method gives the address after providing it with the coordinates.
Syntax:
reverse(latitude,longitude)
Approach
- Import module
- Call nominatim tool
- Pass latitude and longitude to reverse()
- Print location name
Example:
Python3
# importing modules
from geopy.geocoders import Nominatim
# calling the nominatim tool
geoLoc = Nominatim(user_agent="GetLoc")
# passing the coordinates
locname = geoLoc.reverse("26.7674446, 81.109758")
# printing the address/location name
print(locname.address)
Output:

Similar Reads
How to get city name by using geolocation ? In this article, we will learn how to get the city name of the user using reverse geocoding. We will be using a 3rd party API service provider(Location IQ) to help us to convert the latitude and longitude we receive into an address. Steps: Get user coordinates. Get city name. Note: A LocationIQ acco
3 min read
How to get value from address in Python ? In this article, we will discuss how to get the value from the address in Python. First, we have to calculate the memory address of the variable or python object which can be done by using the id() function. Syntax: id(python_object) where, python_object is any python variable or data structure like
4 min read
HTML Geolocation watchPosition() Method In this article, we will discuss the Geolocation watchPosition() method. Geolocation in HTML is used to register a handler function that will be called automatically each time the position of the device changes. Syntax: navigator.geolocation.watchPosition(showLoc, error, options); Parameter: showLoc
3 min read
Python time altzone() Method Python altzone() method is used to return the offset of the local DST timezone, in seconds west of UTC. We can also get the current time between the current time zone and UTC by using the timezone and altzone method. Syntax: time.altzone Example: Python3 # import the time module import time # displa
1 min read
HTML Geolocation getCurrentPosition() Method In this article, we will know HTML Geolocation getCurrentPosition() Method, various parameters, & their implementation through the examples. HTML5 provides us the Geolocation API which helps us to identify the geographic location of the user. The Geolocation API gives the latitude and longitude
2 min read