GoogleMaps API -address to coordinates (latitude,longitude)

后端 未结 3 1396
被撕碎了的回忆
被撕碎了的回忆 2021-02-02 15:54

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         


        
3条回答
  •  [愿得一人]
    2021-02-02 16:04

    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, ""
        return latitude, longitude
    

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

提交回复
热议问题