In this article we will discuss on how to get Geolocation when you enter any location name and its 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 installed explicitly.
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 ot express the location in terms of coordinates.
- Import module
- Import Nominatim from geopy- Nominatim is a free service or tool or can be called as API with no keys that provides you with the data after providing it with name and address and vice versa.
- On calling 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
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.
- Import module
- Call nominatim tool
- Pass latitude and longitude to reverse()
- Print location name
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course