Python – How To Get Latitude & Longitude with python

google mapspythonpython-2.7

I am trying to retrieve the longitude & latitude of a physical address ,through the below script .But I am getting the error. I have already installed googlemaps .
kindly reply
Thanks In Advance

#!/usr/bin/env python
import urllib,urllib2

"""This Programs Fetch The Address"""

from googlemaps import GoogleMaps

address='Mahatma Gandhi Rd, Shivaji Nagar, Bangalore, KA 560001'

print add


Traceback (most recent call last):
  File "", line 12, in <module>
  File "/usr/local/lib/python2.7/dist-packages/", line 310, in address_to_latlng
    return tuple(self.geocode(address)['Placemark'][0]['Point']['coordinates'][1::-1])
  File "/usr/local/lib/python2.7/dist-packages/", line 259, in geocode
    url, response = fetch_json(self._GEOCODE_QUERY_URL, params=params)
  File "/usr/local/lib/python2.7/dist-packages/", line 50, in fetch_json
    response = urllib2.urlopen(request)
  File "/usr/lib/python2.7/", line 127, in urlopen
    return, data, timeout)
  File "/usr/lib/python2.7/", line 407, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/", line 520, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/", line 445, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/", line 379, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/", line 528, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden

Best Answer

googlemaps package you are using is not an official one and does not use google maps API v3 which is the latest one from google.

You can use google's geocode REST api to fetch coordinates from address. Here's an example.

import requests

response = requests.get(',+Mountain+View,+CA')

resp_json_payload = response.json()