Skip to content

Instantly share code, notes, and snippets.

@gtarawneh
Created March 18, 2020 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gtarawneh/893bfe5c0b87b89df891a60868e2047e to your computer and use it in GitHub Desktop.
Save gtarawneh/893bfe5c0b87b89df891a60868e2047e to your computer and use it in GitHub Desktop.
import json
import requests
"""
Simple tool to fetch the lat, lng coordinates of a list of addresses.
Requirements:
1. This requires an API_KEY for the Google Maps Platform which can be obtained
by following the instructions here ...
https://developers.google.com/maps/documentation/geocoding/get-api-key
2. The tool requires "requests", a popular Python package, which can be
installed with "pip install requests".
"""
API_KEY = "google-maps-geocoding-api-key"
url = "https://maps.googleapis.com/maps/api/geocode/json"
addresses = [
"2 Hawthorn Gardens, Newcastle upon Tyne, UK",
"3 Hawthorn Gardens, Newcastle upon Tyne, UK",
"4 Hawthorn Gardens, Newcastle upon Tyne, UK",
"5 Hawthorn Gardens, Newcastle upon Tyne, UK",
"6 Hawthorn Gardens, Newcastle upon Tyne, UK",
"7 Hawthorn Gardens, Newcastle upon Tyne, UK",
"8 Hawthorn Gardens, Newcastle upon Tyne, UK",
"9 Hawthorn Gardens, Newcastle upon Tyne, UK",
"10 Hawthorn Gardens, Newcastle upon Tyne, UK",
]
def get_location(address):
"""Return the location of an address.
Inputs:
address (str): e.g. "1 Hawthorn Gardens,Newcastle upon Tyne, UK"
Returns:
location (tuple): (lat, lng)
"""
params = {"key": API_KEY, "address": address}
repsonse = requests.get(url, params)
results = repsonse.json()["results"]
location = results[0]["geometry"]["location"]
return (location["lat"], location["lng"])
def main():
for address in addresses:
location = get_location(address)
print(location)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment