The Wayback Machine - https://web.archive.org/web/20240605213720/https://www.geeksforgeeks.org/how-to-get-geolocation-in-python/
Open In App

How to get Geolocation in Python?

Last Updated : 06 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

Python Dictionary get() Method
Python Dictionary get() Method return the value for the given key if present in the dictionary. If not, then it will return None (if get() is used with only one argument). Python Dictionary get() Method Syntax: Syntax : Dict.get(key, default=None) Parameters: key: The key name of the item you want to return the value fromValue: (Optional) Value to
2 min read
GET and POST Requests Using Python
This post discusses two HTTP (Hypertext Transfer Protocol) request methods GET and POST requests in Python and their implementation in Python. What is HTTP? HTTP is a set of protocols designed to enable communication between clients and servers. It works as a request-response protocol between a client and a server. A web browser may be the client,
5 min read
Python | Get indices of True values in a binary list
Boolean lists are often used by developers to check for True values during hashing. The boolean list is also used in certain dynamic programming paradigms in dynamic programming. Let's discuss certain ways to get indices of true values in a list in Python. Method #1 : Using enumerate() and list comprehension enumerate() can do the task of hashing i
9 min read
Python | Get a set of places according to search query using Google Places API
Google Places API Web Service allow the user to query for place information on a variety of categories, such as establishments, prominent points of interest, geographic locations, and more. One can search for places either by proximity or a text string. A Place Search returns a list of places along with summary information about each place; additio
3 min read
Python | Get unique values from a list
In this article, we will explore various techniques and strategies for efficiently extracting distinct elements from a given list. By delving into methods ranging from traditional loops to modern Pythonic approaches with Python. Input : [1,2, 1, 1, 3, 4, 3, 3, 5 ]Output : [1, 2, 3, 4, 5] Explaination: The output only contains the unique element fro
7 min read
Get emotions of images using Microsoft emotion API in Python
The emotions of images like happy, sad, neutral, surprise, etc. can be extracted using Microsoft emotion API for any development purpose. It is very simple to use and can be called via API through terminal or any of languages like Python or PHP. Microsoft provides free subscription of 30 days for making total of 30,000 requests. The details of the
2 min read
Python | Get positive elements from given list of lists
Given a list of list, the task is to get only positive element from given list. Below are some ways to solve the above problem. Method #1: Using Iteration C/C++ Code # Python code to get positive # element from list of list # List Initialisation Input = [[10, -11, 222], [42, -222, -412, 99, -87]] Output = [] # Using iteration for elem in Input: tem
5 min read
How to Get the Minimum and maximum Value of a Column of a MySQL Table Using Python?
Prerequisite: Python: MySQL Create Table In this article, we are going to see how to get the Minimum and Maximum Value of a Column of a MySQL Table Using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python module is an
2 min read
Get similar words suggestion using Enchant in Python
For the given user input, get similar words through Enchant module. Enchant is a module in python which is used to check the spelling of a word, gives suggestions to correct words. Also, gives antonym and synonym of words. It checks whether a word exists in dictionary or not. Other dictionaries can also be added, as, ("en_UK"), ("en_CA"), ("en_GB")
1 min read
Get all text of the page using Selenium in Python
As we know Selenium is an automation tool through which we can automate browsers by writing some lines of code. It is compatible with all browsers, Operating systems, and also its program can be written in any programming language such as Python, Java, and many more. Selenium provides a convenient API to access Selenium WebDrivers like Firefox, IE,
3 min read