Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
52 lines (42 loc) · 1.65 KB

File metadata and controls

52 lines (42 loc) · 1.65 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# Michael Galarnyk
# Assignment 7
# “Google Geocoding”
# 1. Change either the geojson.py or geoxml.py program to print out the two-character country code from the retrieved data.
# 2. Add error checking so your program does not traceback if the country code is not there.
# 3. Use the program to search for “Pacific Ocean” and make sure that it can handle locations that
# are not in any country.
import urllib
import json
serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'
while True:
address = raw_input('Enter location: ')
if len(address) < 1 : break
url = serviceurl + urllib.urlencode({'sensor':'false', 'address': address})
print 'Retrieving', url
uh = urllib.urlopen(url)
data = uh.read()
print 'Retrieved',len(data),'characters'
try: js = json.loads(str(data))
except: js = None
if 'status' not in js or js['status'] != 'OK':
print '==== Failure To Retrieve ===='
print data
continue
print json.dumps(js, indent=4)
'''
lat = js["results"][0]["geometry"]["location"]["lat"]
lng = js["results"][0]["geometry"]["location"]["lng"]
print 'lat',lat,'lng',lng
''' #not necessary for this assignment
location = js['results'][0]['formatted_address']
print location
results = js['results'][0]
address_components = results["address_components"]
country = 0;
for each_dict in address_components:
types = each_dict["types"]
if types == ["country", "political"]:
country = 1;
print "The two character country code is:", each_dict["short_name"]
if country == 0:
print "Location isn't in any country"
Morty Proxy This is a proxified and sanitized view of the page, visit original site.