USING PYTHON FOR ACCESSING GOOGLE GEOAPI

For page specific messages
For page specific messages

Google API is a set of application programming interfaces  developed by Google which allow communication with Google Services and their integration to other services. Examples of these include Search, Gmail, Translate or Google Maps. Third-party apps can use these APIs to take advantage of or extend the functionality of the existing services.

The APIs provide functionality like analytics, machine learning as a service (the Prediction API) or access to user data (when permission to read the data is given). Another important example is an embedded Google map on a website, which can be achieved using the Static maps API, Places API or Google Earth API.

Though a lot of websites contains scattered code which can be used for accessing google map api, there is no one place where we can get all the codes required for using the api. So, here we tried to make your life easier by sharing the end to end code.

The code takes location as input from user and print its longitude and latitude  from google api. Python is indent aware, so keep indent in mind while using this code. Happy Coding.

 

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
    location = js[‘results’][0][‘formatted_address’]
    print location

 

 

Please feel free to comment if you face any problem in using the code. Our next post will be accessing the Twitter API. So, stay tuned and stay updated. 

 

 

Exclude node summary : 

n