-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractice of Geocoding API and JSON.py
More file actions
70 lines (62 loc) · 2.11 KB
/
Practice of Geocoding API and JSON.py
File metadata and controls
70 lines (62 loc) · 2.11 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import urllib.request, urllib.parse, urllib.error
import json
serviceURL = "http://maps.googleapis.com/maps/api/geocode/json?address="
while True:
'''Because now Google Geocoding API require a API key,
we could not use the code below to fetch the JSON code anymore;
Therefore, I attached the JSON code below directly.'''
# address = input("Enter the location: ")
# if len(address) < 1:
# break
#
# finalURL = serviceURL + urllib.parse.urlencode({'address', address})
# # Get the address of the location and transform it into the right form.
# print("Retrieving", url)
# rawData = urllib.request.urlopen(finalURL)
# data = rawData.read().decode()
# print("Retrieving", len(data), "characters")
# # Fetch the data from the server.
data = '''{
"status": "OK",
"result": [
{
"geometry": {
"location_type": "APPROXIMATE",
"location": {
"lat": 25,
"lng": 0
}
},
"address_components": [
{
"long_name": "Columbia University",
"types": [
"locality",
"political"
],
"short_name": "Columbia University"
}
],
"formatted_address": "Columbia University, NYU, USA",
"types": [
"locality",
"political"
]
}
]
}'''
try:
js = json.loads(data)
# Identify wgether the data we get is JSON or not.
except:
js = None
if not js or 'status' not in js or js['status'] != 'OK':
print("=====Failure to fetch the data!=====")
print(data)
continue
print(json.dumps(js, indent = 4)) # Organize the JSON data.
latitude = js['result'][0]['geometry']['location']['lat']
longtitude = js['result'][0]['geometry']['location']['lng']
finalAddress = js['result'][0]['formatted_address']
print("The location of the place-" + finalAddress + "-is at (" + str(latitude) + ", " + str(longtitude) + ").")
break