Last active
July 7, 2019 16:05
-
-
Save alisonamerico/7a219077220c2e64e993bb65b3ca8f64 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from app.settings import GOOGLE_API_KEY, GOOGLE_API_HOST | |
location = input("What's your location? ") | |
# Geocoding | |
resp = requests.get(GOOGLE_API_HOST, params={ | |
'address': location, | |
'key': GOOGLE_API_KEY | |
}) | |
if resp.status_code == 200: | |
print('\n') | |
resp_dict = resp.json() | |
print('# Latitude and Longitude') | |
lat = (resp_dict['results'][0]['geometry']['location']['lat']) | |
lng = (resp_dict['results'][0]['geometry']['location']['lng']) | |
print(f'Latitude: {lat}, Longitude: {lng}') | |
else: | |
print('Your Request in Geocoding Fail') | |
# Reverse Geocoding | |
resp = requests.get(GOOGLE_API_HOST, params={ | |
'latlng': f'{lat},{lng}', | |
'key': GOOGLE_API_KEY | |
}) | |
if resp.status_code == 200: | |
print('\n') | |
print('# Most Accurate Address') | |
resp_dict = resp.json() | |
print(resp_dict['results'][0]['formatted_address']) | |
print(resp_dict['results'][0]['types'][0]) | |
if len(resp_dict['results']) > 1: | |
print('\n') | |
print('# Least Accurate Address') | |
print(resp_dict['results'][len(resp_dict['results']) - 1] | |
['formatted_address']) | |
print(resp_dict['results'] | |
[len(resp_dict['results']) - 1]['types'][0]) | |
else: | |
print('Your Request in Reverse Geocoding Fail') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment