Calling Google Geocoding Web Service
import urllib.request, urllib.parse, urllib.error
import json
# Note that Google is increasingly requiring keys for this API
serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'
address = input('Enter location: ')
if len(address)>0:
url = serviceurl + urllib.parse.urlencode(
{'address': address})
print('Retrieving', url)
uh = urllib.request.urlopen(url)
data = uh.read().decode()
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)
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)
location = js['results'][0]['formatted_address']
print(location)
else:
print("Invlid Address")
import json
# Note that Google is increasingly requiring keys for this API
serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'
address = input('Enter location: ')
if len(address)>0:
url = serviceurl + urllib.parse.urlencode(
{'address': address})
print('Retrieving', url)
uh = urllib.request.urlopen(url)
data = uh.read().decode()
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)
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)
location = js['results'][0]['formatted_address']
print(location)
else:
print("Invlid Address")
Output
Enter location: Impeccable IT Academy
Retrieving http://maps.googleapis.com/maps/api/geocode/json?address=Impeccable+IT+Academy
Retrieved 1736 characters
{
"results": [
{
"address_components": [
{
"long_name": "Shastri Nagar",
"short_name": "Shastri Nagar",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "Ghaziabad",
"short_name": "Ghaziabad",
"types": [
"locality",
"political"
]
},
{
"long_name": "Uttar Pradesh",
"short_name": "UP",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "India",
"short_name": "IN",
"types": [
"country",
"political"
]
},
{
"long_name": "201002",
"short_name": "201002",
"types": [
"postal_code"
]
}
],
"formatted_address": "2nd Floor, SE-387A, Near Petrol Pump Opp. CBI Academy, Shastri Nagar, Ghaziabad, Uttar Pradesh 201002, India",
"geometry": {
"location": {
"lat": 28.6761113,
"lng": 77.4702933
},
"location_type": "GEOMETRIC_CENTER",
"viewport": {
"northeast": {
"lat": 28.6774602802915,
"lng": 77.4716422802915
},
"southwest": {
"lat": 28.6747623197085,
"lng": 77.4689443197085
}
}
},
"place_id": "ChIJB36akSDyDDkRgc3ZjZRrO2M",
"types": [
"establishment",
"point_of_interest"
]
}
],
"status": "OK"
}
lat 28.6761113 lng 77.4702933
2nd Floor, SE-387A, Near Petrol Pump Opp. CBI Academy, Shastri Nagar, Ghaziabad, Uttar Pradesh 201002, India
Comments
Post a Comment