Google Geocode Web Service (JSON response)

Python Methods and Functions

The geocoding service is free but has a limited speed, so you cannot use the API indefinitely in a commercial application. But if you have survey data where the end user has entered a location in a freeform input field, you can use this API to clean up your data pretty well.

You can read the online documentation on this service, but it's pretty simple and you can even test it with a browser by entering the following URL into your browser:
http://maps.googleapis.com/maps/api/geocode/json?address=Ann+Arbor%2C+MI

Make sure to unpack the URL and remove any spaces from the URL before pasting it into the browser.

Below is a simple application that prompts the user for a search string, calls the Google Geocoding API, and extracts information from the returned JSON.

# Libraries used to capture web content URL and json import

import urllib.request, urllib.parse, urllib.error

import json

  
# Please note that Google increasingly requires keys
# for this API
# Service URL for Google Maps API

serviceurl = ' http://maps.googleapis.com/maps/api/geocode/json? '

 

while True :

address = input ( 'Enter location:' )

  if len (address) & lt;  1 : break

 

# Combine serviceurl and urllib.parse.urlencode

# which give equal addresses and this bit

# Right here

url = serviceurl + urllib.parse.urlencode (

{ 'address' : address})

  

print  ( 'Retrieving' , url)

  

# urlopen () to get the descriptor

uh = urllib. request.urlopen (url)

# Read entire document in UTF-8

data = uh.read (). decode ()

print ( 'Retrieved' , len (data), 'characters' )

  

  # Load inner lines

try :

js = json.loads (data)

except :

js = None

# If false, exit and print data

if not js or ' status' not   in js or js [ 'status' ]! = ' OK' :

print ( '==== Failure To Retrieve ====' )

  print (data )

continue

  

  # Call json dump and print it indented four

print (json.dumps (js, indent = 4 ))

  

# Parse and print

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 )

Exit:

 Enter location : Dehradun Retrieving http://maps.googleapis.com/maps/api/geocode/json?address=dehradun Re trieved 1743 characters {"results": [{"address_components": [{"long_name": "Dehradun", "short_name": "Dehradun", "types": ["locality", "political"]}, {"long_name ":" Dehradun "," short_name ":" Dehradun "," types ": [" administrative_area_level_2 "," political "]}, {" long_name ":" Uttarakhand "," short_name ":" UK "," types ": ["administrative_area_level_1", "political"]}, {"long_name": "India", "short_name": "IN", "types": ["country", "political"]}], "formatted_address": "Dehradun , Uttarakhand, India "," geometry ": {" bounds ": {" northeast ": {" lat ": 30.4041936," lng ": 78.1089305}," southwest ": {" lat ": 30.2466633," lng ": 77.92533879999999 }}, "location": {"lat": 30.3164945, "lng": 78.03219179999999}, "location_type": "APPROXIMATE", "viewport": {"northeast": {"lat": 30.4041936, "lng": 78.1089305 }, "southwest": {"lat": 30.2466633, "lng": 77.92533879999999}}}, "place_id": "ChIJr4jIVsMpCTkRmYdRMsBiNUw", "types": ["locality", "political"]}], "status": "OK"} lat 30.3 164945 lng 78.03219179999999 Dehradun, Uttarakhand, India 

The program takes the search string and creates a URL with the search string as a correctly encoded parameter, and then uses urllib to retrieve the text from the Google Geocoding API. Unlike a fixed web page, the data we receive depends on the parameters we send and the geographic data stored on Google's servers. 
Once we receive the JSON data, we parse it with the json library and do a few checks to make sure we got good data, then fetch the information we're looking for.





Tutorials