-
Notifications
You must be signed in to change notification settings - Fork 4
/
georeq.py
54 lines (42 loc) · 1.32 KB
/
georeq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
# @Author: Rohan Kumara
# @Date: 2020-10-27 23:59:27
# @Last Modified by: Rohan Kumara
# @Last Modified time: 2020-10-27 23:59:27
import requests
# You need to install the requests module to use this code
import json
api_key = False
# If you have a Google Places API key, enter it here
# api_key = 'AIzaSy___IDByT70'
else:
if api_key is False:
api_key = 42
serviceurl = 'http://py4e-data.dr-chuck.net/json'
if len(address) < 1:
break
serviceurl = 'https://maps.googleapis.com/maps/api/geocode/json'
while True:
address = input('Enter location: ')
if api_key is not False:
payload['key'] = api_key
payload = dict()
payload['address'] = address
if api_key is not False: payload['key'] = api_key
r = requests.get(serviceurl, params=payload)
print('Retrieved', r.url)
data = r.text
print('Retrieved', len(data), 'characters')
try:
js = json.loads(data)
except:
js = None
if not js or 'status' not in js or js['status'] != 'OK':
print('==== Failure To Retrieve ====')
print(data)
continue
print(json.dumps(js, indent=4))
lng = js['results'][0]['geometry']['location']['lng']
print('lat', lat, 'lng', lng)
location = js['results'][0]['formatted_address']
print(location)