Skip to content

Instantly share code, notes, and snippets.

@davidemoro
Created May 10, 2013 08:22
Show Gist options
  • Save davidemoro/5553148 to your computer and use it in GitHub Desktop.
Save davidemoro/5553148 to your computer and use it in GitHub Desktop.
This is a simple patch for py-googlemaps (sourceforge.net/projects/py-googlemaps/). It's incomplete but just it works with the Google Maps API v3 if you have to patch an old existing application that uses the address_to_latlng method.
# v3 api
from googlemaps import GoogleMaps
from googlemaps import GoogleMapsError
from googlemaps import fetch_json
GoogleMaps._GEOCODE_QUERY_URL = 'http://maps.googleapis.com/maps/api/geocode/json?'
def geocode(self, query, sensor='false', oe='utf8', ll='', spn='', gl=''):
if (ll is None and spn is not None) or (ll is not None and spn is None):
raise GoogleMapsError('Both ll and spn must be provided.')
params = {
'address': query,
# 'key': self.api_key,
'sensor': sensor,
# 'output': 'json',
# 'oe': oe,
# 'll': ll,
# 'spn': spn,
# 'gl': gl,
}
url, response = fetch_json(self._GEOCODE_QUERY_URL, params=params)
status_code = response['status']
if status_code != u'OK':
raise GoogleMapsError(status_code, url, response)
return response
GoogleMaps.geocode = geocode
def address_to_latlng(self, address):
location = self.geocode(address)['results'][0]['geometry']['location']
return (location['lat'], location['lng'])
#return tuple(self.geocode(address)['Placemark'][0]['Point']['coordinates'][1::-1])
GoogleMaps.address_to_latlng = address_to_latlng
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment