How to Get Geolocation in Python?

Posted in /  

How to Get Geolocation in Python?
vinaykhatri

Vinay Khatri
Last updated on November 24, 2024

    In Geolocation computing, we generally find out the longitude and latitude of a location from its physical address and vise versa. The longitude and latitude are the geographic coordination of an address on the Earth's surface. And there are many applications such as Google Maps, Mapbox, MapQuest, etc., use geolocation coordinates to tell the Physical address of the place or location.

    In this Python tutorial, we will write the Python code to find the Geolocation coordinates latitude, and longitude for a Physical Address. We will also see how to find out the current Geolocation using Python. Before diving into the Python code implementation, let's install the required Libraries.

    Install Libraries

    Python geopy library

    To find the geolocation coordinates for a Physical address we will use the geopy library. The geopy is the open-source library which is generally used to locate the coordinates of addresses, cities, countries, and landmarks. Using the Python pip terminal command you can install the geopy library for your Python environment.

    pip install geopy

    Python geocoder library

    The Python geocoder is also a geocoding library, and it can also be used for finding the geo-coordinates for an address. In this tutorial, we will be using this library to find out our current geolocation. Run the following pip install command on your terminal or command prompt to install the Python geocoder library.

    pip install geocoder

    Now open your best Python Ide and Code Editor and start coding with me.

    Python Program to find the Latitude and Longitude of an Address

    Let's find the latitude and longitude for the address " Delhi Connaught Place". Although the Python geopy library supports multiple GeoCoding APIs including Google, for this tutorial, we will be using its Nominatim module. If you wish to use the Google API module you need to create an API first, so it's better to use the Nominatim module.

    Python code:

    from geopy.geocoders import Nominatim
    
    #initialize the Nominatim object
    Nomi_locator = Nominatim(user_agent="My App")
    
    address= "Delhi Connaught Place"
    
    #get the location detail 
    location = Nomi_locator.geocode(address)
    
    print("You find for the location:", location)
    print(f"The Latitude of {location} is: <{location.latitude}>")
    print(f"The Longitude of {location} is: <{location.longitude}>")

    Output:

    You find for the location: Connaught Place, Chanakya Puri Tehsil, New Delhi, Delhi, 110001, India
    The Latitude of Connaught Place, Chanakya Puri Tehsil, New Delhi, Delhi, 110001, India is: <28.6313827>
    The Longitude of Connaught Place, Chanakya Puri Tehsil, New Delhi, Delhi, 110001, India is: <77.2197924>

    From the output, you can see that the geolocator returns the complete address for the specified address. In our specified address we did not specify the country, it's all the Geolocator that searched through all the possible matches and return us the correct address. you can also Google to cross-check if the returned latitude and longitude are correct.

    Python Program to find the Address from the longitude and latitude.

    Now you know how to find the longitude and latitude of a physical address using Python. Now we will write a Python program that will find the address for a specific latitude and longitude. To find the Latitude and Longitude coordinates we used the Nomi_locator.geocode() function with the address as an argument, to find the address we will use the Nomi_locator.reverse() method with latitude and longitude as parameters.

    Python code

    from geopy.geocoders import Nominatim
    
    #initialize the Nominatim object
    Nomi_locator = Nominatim(user_agent="My App")
    
    #latitude and longitude for Mumbai India
    latitude = "19.0760"
    longitude= "72.8777"
    
    #get the location
    location = Nomi_locator.reverse(f"{latitude}, {longitude}")
    print("You find for the location:", location)

    Output:

    You find for the location: Santa Cruz – Chembur Link Road, Hallow Pul, L Ward, Zone 5, Mumbai, Mumbai Suburban, Maharashtra, 400070, India

    Python Program to Get Your Location

    To get our current location we will use the Python geocoder library. And with the geocoder ip() method we will find out the current location of our Internet Protocol address.

    Python Code

    from geopy.geocoders import Nominatim
    import geocoder
    
    #initialize the Nominatim object
    Nomi_locator = Nominatim(user_agent="My App")
    
    my_location= geocoder.ip('me')
    
    #my latitude and longitude coordinates
    latitude= my_location.geojson['features'][0]['properties']['lat']
    longitude = my_location.geojson['features'][0]['properties']['lng']
    
    #get the location
    location = Nomi_locator.reverse(f"{latitude}, {longitude}")
    print("Your Current IP location is", location)

    Output

    Your Current IP location is Punjab & Sind Bank, Deshbandhu Gupta Road, Multani Dhanda, Paharganj, Karol Bagh Tehsil, Central Delhi, Delhi, 110055, India

    Conclusion

    In this Python tutorial, you learned how to find out the Geo Coordinates from a Physical address and vice versa. You also learned how to find out the current IP address location for your device in Python. I would recommend you read the official documentation of GeoPy and geocoder library to know more about their API working.

    People are also reading:

    Leave a Comment on this Post

    0 Comments