Python to Fetch Latitude and Longitude of Your Website with Full Source Code

Geocoding Script

  • This script takes an address and return its latitude and longitude.This process is called geocoding
  • I have used the locationiq website’s geocoding api inorder to solve this problem.
  • To be able to use this script you have to create a free account at locationiq and obtain your private token.

Remember, don’t share your private token with anyone.

Requirements:

  • certifi==2020.6.20
  • chardet==3.0.4
  • idna==2.10
  • requests==2.24.0
  • urllib3==1.26.5

Source Code:

geocoding.py

import requests
# Base Url for geocoding
url = "https://us1.locationiq.com/v1/search.php"
address = input("Input the address: ")
#Your unique private_token should replace value of the private_token variable.
#To know how to obtain a unique private_token please refer the README file for this script.
private_token = "Your_private_token"
data = {
    'key': private_token,
    'q': address,
    'format': 'json'
}
response = requests.get(url, params=data)
latitude = response.json()[0]['lat']
longitude = response.json()[0]['lon']
print(f"The latitude of the given address is: {latitude}")
print(f"The longitude of the given address is: {longitude}")
print("Thanks for using this script")Code language: PHP (php)

Leave a Comment