Skip to content

Instantly share code, notes, and snippets.

@thisismattmiller
Created April 6, 2022 21:41
Show Gist options
  • Save thisismattmiller/9f21fd370b784adfe2593e2e2e784b3a to your computer and use it in GitHub Desktop.
Save thisismattmiller/9f21fd370b784adfe2593e2e2e784b3a to your computer and use it in GitHub Desktop.
import requests
import json
source_data = json.load(open('data.json'))
url = 'https://maps.googleapis.com/maps/api/geocode/json'
key = json.load(open('google_key.json'))
for item in source_data:
payload = {
'address' : item['adress'],
'key' : key
}
response = requests.get(url,params=payload)
print(response.status_code)
data = json.loads(response.text)
if 'results' in data:
if (len(data['results']) > 0):
first_result = data['results'][0]
lat = first_result['geometry']['location']['lat']
lng = first_result['geometry']['location']['lng']
print(f"{lat}, {lng}")
item['lat'] = lat
item['lng'] = lng
else:
print("Error, no results")
else:
print("Error, no results key in data")
json.dump(source_data,open('data_with_lat_lng.json','w'),indent=2)
# print(json.dumps(data,indent=2))
import requests
import json
access_token = json.load(open('mapbox_key.json'))
payload = {
'types' : 'address',
'access_token' : access_token
}
lookfor="144 West 14th Street New York, NY 10011"
url = f"https://api.mapbox.com/geocoding/v5/mapbox.places/{lookfor}.json"
response = requests.get(url,params=payload)
print(response.status_code)
data = json.loads(response.text)
# print(json.dumps(data,indent=2))
if 'features' in data:
if (len(data['features']) > 0):
first_result = data['features'][0]
lat = first_result['center'][0]
lng = first_result['center'][1]
print(lat,lng)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment