GoogleMaps API -address to coordinates (latitude,longitude)
Asked Answered
V

3

16

This is driving me crazy. I have deleted this key 1000 times so far. Yesterday it worked like a charm, today not anymore Here is the python code:

from googlemaps import GoogleMaps
gmaps = GoogleMaps("AIzaSyBIdSyB_td3PE-ur-ISjwFUtBf2O0Uo0Jo")
exactaddress ="1 Toronto Street Toronto"
lat, lng = gmaps.address_to_latlng(exactaddress)
print lat, lng

GoogleMapsError: Error 610: G_GEO_BAD_KEY

It is now returning the above error for no obvious reasons. I don't think I have reached the request limit or the maximum rate To stay on the safe side I even introduced delays (1sec) ...stil getting the same error

Does anybody have any idea how I can solve this? Having to work with a different python module is fine if you can indicate an alternative to the one that I am currently using.

thanks C

PS: the key is valid, it is a client key and it was automatically enabled when I enabled GoogleMAP API3 in the App console. No restrictions for domains or IPs

EDIT: So here is what I ended up using

def decodeAddressToCoordinates( address ):
        urlParams = {
                'address': address,
                'sensor': 'false',
        }  
        url = 'http://maps.google.com/maps/api/geocode/json?' + urllib.urlencode( urlParams )
        response = urllib2.urlopen( url )
        responseBody = response.read()

        body = StringIO.StringIO( responseBody )
        result = json.load( body )
        if 'status' not in result or result['status'] != 'OK':
                return None
        else:
                return {
                        'lat': result['results'][0]['geometry']['location']['lat'],
                        'lng': result['results'][0]['geometry']['location']['lng']
                }  

The library that Jason pointed me to is also interesting but since my code was intended to fix something (one time use) I have not tried his solution. I will definitely consider that if I get to write code again :-)

Vigil answered 8/3, 2013 at 2:29 Comment(2)
Not sure if the python implementation is similar, but people using the v2 geocoding were suffering problems because of the end of life of v2. Switching to v3 seemed to cure some problems. Is there a version difference for python?Esposito
Starting March 10, 2013, support for v2 went down, and as far as I know, the googlemaps package seems to be using v2, which has different query addresses, in theory, changing the query address should be enough (in the googlemaps package).Guidotti
E
11

Although Google deprecated the V2 calls with googlemaps (which is why you're seeing the broken calls), they just recently announced that they are giving developers a six-month extension (until September 8, 2013) to move from the V2 to V3 API. See Update on Geocoding API V2 for details.

In the meantime, check out pygeocoder as a possible Python V3 solution.

Espinosa answered 11/3, 2013 at 20:20 Comment(1)
Thanks a lot for the update. I will give it a try. When I saw that nobody could help I googled a little bit more and I found the function pasted in the initial question now, which worked pretty well:Vigil
N
11

Since September 2013, Google Maps API v2 no longer works. Here is the code working for API v3 (based on this answer):

import urllib
import simplejson

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

def get_coordinates(query, from_sensor=False):
    query = query.encode('utf-8')
    params = {
        'address': query,
        'sensor': "true" if from_sensor else "false"
    }
    url = googleGeocodeUrl + urllib.urlencode(params)
    json_response = urllib.urlopen(url)
    response = simplejson.loads(json_response.read())
    if response['results']:
        location = response['results'][0]['geometry']['location']
        latitude, longitude = location['lat'], location['lng']
        print query, latitude, longitude
    else:
        latitude, longitude = None, None
        print query, "<no results>"
    return latitude, longitude

See official documentation for the complete list of parameters and additional information.

Nativity answered 14/3, 2014 at 8:42 Comment(0)
C
1

Did some code golfing and ended up with this version. Depending on your need you might want to distinguish some more error conditions.

import urllib, urllib2, json

def decode_address_to_coordinates(address):
        params = {
                'address' : address,
                'sensor' : 'false',
        }  
        url = 'http://maps.google.com/maps/api/geocode/json?' + urllib.urlencode(params)
        response = urllib2.urlopen(url)
        result = json.load(response)
        try:
                return result['results'][0]['geometry']['location']
        except:
                return None
Conversationalist answered 14/8, 2014 at 10:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.