Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Get Latitude & Longitude with python

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'  add=GoogleMaps().address_to_latlng(address) print add 

Output:

Traceback (most recent call last):   File "Fetching.py", line 12, in <module>     add=GoogleMaps().address_to_latlng(address)   File "/usr/local/lib/python2.7/dist-packages/googlemaps.py", 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/googlemaps.py", line 259, in geocode     url, response = fetch_json(self._GEOCODE_QUERY_URL, params=params)   File "/usr/local/lib/python2.7/dist-packages/googlemaps.py", line 50, in fetch_json     response = urllib2.urlopen(request)   File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen     return _opener.open(url, data, timeout)   File "/usr/lib/python2.7/urllib2.py", line 407, in open     response = meth(req, response)   File "/usr/lib/python2.7/urllib2.py", line 520, in http_response     'http', request, response, code, msg, hdrs)   File "/usr/lib/python2.7/urllib2.py", line 445, in error     return self._call_chain(*args)   File "/usr/lib/python2.7/urllib2.py", line 379, in _call_chain     result = func(*args)   File "/usr/lib/python2.7/urllib2.py", line 528, in http_error_default     raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 403: Forbidden 
like image 963
user3008712 Avatar asked Sep 17 '14 10:09

user3008712


1 Answers

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('https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA')  resp_json_payload = response.json()  print(resp_json_payload['results'][0]['geometry']['location']) 
like image 184
Saleem Latif Avatar answered Sep 29 '22 05:09

Saleem Latif