How to get Geolocation in Python?


Numerous libraries that offer geolocation services are available in Python, notably the geopy module, which enables programmers to geocode and reverse geocode addresses and places. Calculating the distance between two points is made simpler by the geopy package, which also offers distance calculations between two points.

There are several libraries that allow for the manipulation of geographical data in Python, including GeoDjango, GeoPandas, and PyProj. These libraries make it simpler for programmers to manipulate geographic data, such as points, lines, and polygons, making it possible to design applications that call for mapping and spatial analysis.

The geopy library can be used in Python to obtain the geolocation. The following steps to guide yoo to get Geolocation in Python −

Algorithms

  • In your command prompt or terminal, type pip install geopy to install the Geopy library.

  • Import the required Geopy library modules. The Nominatim class is required for geocoding, and the Point class is required to represent a geographic point.

from geopy.geocoders import Nominatim
from geopy.point import Point
  • Create a Nominatim class object, which will enable you to execute geocoding. In order to specify your programme, you can pass in a user agent string.

  • To geocode an address or location, use the geocode method of the Nominatim class. The place you want to geocode is provided as a string by this method.

Syntax

geolocator = Nominatim(user_agent="my_app")
location = geolocator.geocode("1600 Amphitheatre Parkway, Mountain View, CA")
  • A Location object, including details about the geocoded location, is the result of the geocode function. The latitude and longitude properties of the Point object provide access to the location's latitude and longitude coordinates.

Syntax

point = Point(location.latitude, location.longitude)

Approach 1: Obtaining location information from a name

Example – 1

With It is possible to use geopy to extract the coordinates, or latitude and longitude, of a given place. In order to express the location using coordinates, it can be employed.

# importing geopy library
from geopy.geocoders import Nominatim

# calling the Nominatim tool
loc = Nominatim(user_agent="Get Prayagraj")

# entering the location name
getLoc = loc.geocode("Civil Lines Prayagraj")

# printing address
print(getLoc.address)

# printing latitude and longitude
print("Latitude = ", getLoc.latitude, "\n")
print("Longitude = ", getLoc.longitude)

Output

Civil Lines Prayagraj , Uttar Pradesh , 211001 , India
Latitude =25.454479
Longitude = 81.8338255

Approach 2: Identifying a location using its latitude and longitude

Everything about this method is the same as the one above; the only difference is that we will now use the reverse() method, which receives the coordinates (latitude and longitude) as the parameter, in place of the geocode function. This method returns the address after receiving the coordinates.

Example – 2

# importing modules
from geopy.geocoders import Nominatim

# calling the nominatim tool
geoLoc = Nominatim(user_agent="GetPrayagraj")

# passing the coordinates
locname = geoLoc.reverse("26.7674446, 81.109758")

# printing the address/location name
print(locname.address)

Output

Vinayak City Centre , Tashkant Marg , Civil Lines , Prayagraj , Uttar Pradesh , 211001 , India

Conclusion

The Google Maps API is one of the most well-liked and user-friendly methods for obtaining geolocation data in Python, however there are other options. We can quickly get the longitude and latitude coordinates for any address by installing the googlemaps library and giving a valid API key. The OpenStreetMap API and the GeoPy library are other possibilities.

security, and flexibility. By following a few simple steps, developers can define custom decorators and apply them to views to modify their behavior or enforce access control.

Updated on: 21-Jul-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements