GoogleMaps API -address to coordinates (latitude,longitude)

后端 未结 3 1401
被撕碎了的回忆
被撕碎了的回忆 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 15:57

    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
    

提交回复
热议问题