HTTP Error 403 with api_id in accessing Google Maps

前端 未结 3 872
感情败类
感情败类 2020-12-18 15:13

I am planning to use the package \"googlemaps\" in Python and had trouble with the api_id.

The following codes:

from googlemaps import GoogleMaps
gma         


        
相关标签:
3条回答
  • 2020-12-18 15:55

    Api v2 was closed down on sept 13, 2013. Your question was asked on 14th - so that migh be a clue :) . What I managed to do is simply modify URLs in googlemaps.py and now it's working OK for routing.

    I changed _DIRECTIONS_QUERY_URL (line 165 of googlemaps.py) to:

    _DIRECTIONS_QUERY_URL = 'http://maps.googleapis.com/maps/api/directions/output?'
    

    And it worked fine. Also I tried modiying line 164: _GEOCODE_QUERY_URL = 'http://maps.googleapis.com/maps/api/geocode/output?' as suggested here but there's some error, and a) I don't need it anyway b) it's already covered by pygeocoder which is a bit more advanced that googlemaps.py

    0 讨论(0)
  • 2020-12-18 16:04

    You can try using geoPy package in python. Sample code is as below:

    from geopy.geocoders import Nominatim
    geolocator = Nominatim()
    location = geolocator.geocode("Ratainda")
    print((location.latitude, location.longitude))
    
    0 讨论(0)
  • 2020-12-18 16:08

    I faced the same error as you, and didn't find a satisfying answer, so I changed my script following this method found on this site to create the script I needed (http://www.portailsig.org/content/python-geocodage-geolocalisation -- However written in french)

    import urllib, json, csv
    
    def geocode(addr):
        url = "http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false" %   (urllib.quote(addr.replace(' ', '+')))
        data = urllib.urlopen(url).read()
        info = json.loads(data).get("results")[0].get("geometry").get("location")  
        #A little ugly I concede, but I am open to all advices :) '''
        return info
    
    #Open the List file of adresses to look for corresponding lat and lng. 
    f = open('list', 'rb')
    addresses = f.readlines()
    f.close()
    
    #Loop to feed the func with adresses and output the lat & lng.
    for a in addresses:
        r = geocode(a)
        print "%s %s" % (r['lat'], r['lng'])
    

    It works fine for me, except index bug sometimes that I have to fix.

    0 讨论(0)
提交回复
热议问题