In this Python tutorial, we are going to learn how to use Google’s Geocoding API to convert addresses to geographic coordinates.
If you have never used Google Geocoding API before. Google Geocoding API is an API provides a direct and painless way to convert an address to coordinates (latitude, longitude) via an HTTP request.
Source Code:
import requests
API_KEY = '<Your API Key>'
def getGeoCoord(address):
params = {
'key': API_KEY,
'address': address.replace(' ', '+')
}
base_url = 'https://maps.googleapis.com/maps/api/geocode/json?'
response = requests.get(base_url, params=params)
data = response.json()
if data['status'] == 'OK':
result = data['results'][0]
location = result['geometry']['location']
return location['lat'], location['lng']
else:
return
#python #api