Get GeoLocation in Python

Python makes working with geolocation simple and effective. Whether you want to find the coordinates of an address, get an address from latitude and longitude, or detect location by IP, Python provides reliable tools to achieve this. In this tutorial, you will learn different methods to get geolocation in Python with reusable functions and practical examples.

What is Geo Location

Geolocation is the process of identifying the physical location of a person, device, or place using data like addresses, GPS coordinates, or IP addresses.

Use Cases of Geo Location

You can use geolocation to make applications smarter and more user-friendly. By knowing how to get geolocation in Python, you can:

  • Track delivery orders in real time
  • Build ride-sharing apps that depend on user location
  • Suggest nearby restaurants or stores
  • Detect fraud by verifying user IP locations
  • Add maps and navigation features to apps

Install Required Packages

You will need the following Python packages for working with geolocation so before moving forward let’s install these. Open your terminal and enter below commands:

pip install geopy
pip install geocoder

It will take sometime to install. Once installed, we can utilize it into our python script.

You can use geopy to work with addresses and coordinates, such as finding latitude and longitude or converting them back to an address. On the other hand, geocoder is useful for IP-based geolocation, allowing you to detect a user’s city, state, and country from their IP address.

Get Geolocation by Address

The geopy library helps you work with addresses and coordinates in Python. You can get latitude and longitude from an address or turn coordinates back into a readable address. This is useful for maps, delivery apps, and location tracking.

from geopy.geocoders import Nominatim

def get_coordinates_from_address(address):
    geolocator = Nominatim(user_agent="geo_app")
    location = geolocator.geocode(address)
    if location:
        return location.latitude, location.longitude
    return None

result = get_coordinates_from_address("New York City")
print("Coordinates:", result)

Output:

Coordinates: (40.7127281, -74.0060152)

Getting Address from Latitude and Longitude

With Python, you can get an address from latitude and longitude using reverse geocoding. For example, coordinates like 40.7128, -74.0060 return the full address of New York City. This is useful for maps, delivery apps, and ride-sharing services.

from geopy.geocoders import Nominatim

def get_address_from_coordinates(lat, lon):
    geolocator = Nominatim(user_agent="geo_app")
    location = geolocator.reverse((lat, lon))
    if location:
        return location.address
    return None

result = get_address_from_coordinates(40.7128, -74.0060)
print("Address:", result)

Output :

Address: New York, Manhattan Community Board 1, Manhattan, New York County, City of New York, New York, United States

IP Geolocation in Python

The geocoder library is best for finding location details from an IP address. It can quickly return the city, state, and country, making it useful for websites, apps, and fraud detection.

import geocoder

def get_location_from_ip(ip="me"):
    g = geocoder.ip(ip)
    if g.ok:
        return {
            "ip": ip,
            "city": g.city,
            "state": g.state,
            "country": g.country
        }
    return None

result = get_location_from_ip()
print("IP Location:", result)

Output:

IP Location: {'ip': '8.8.8.8', 'city': 'Mountain View', 'state': 'California', 'country': 'US'}

Conclusion

By now, you have learned different methods to get geolocation in Python. You can find coordinates from an address, get addresses from latitude and longitude, and detect location from an IP address. With these Python functions, you can integrate geolocation into your projects easily and reuse the code whenever needed.

If you want to explore more Python example, you can also check out our guide on Download YouTube Videos Using Python.