Get location coordinates using bing or google API in python

前端 未结 3 684
北恋
北恋 2020-12-30 17:19

Here is my problem. I have a sample text file where I store the text data by crawling various html pages. This text contains information about various events and its time an

3条回答
  •  星月不相逢
    2020-12-30 17:57

    Since September 2013, Google Maps API v2 no longer works. Here is an updated version of great @jimhark's code, working for API v3 (I left out the __main__ part):

    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.

提交回复
热议问题