2

我打算在 Python 中使用包“googlemaps”并且遇到了 api_id 问题。

以下代码:

from googlemaps import GoogleMaps
gmaps = GoogleMaps(api_key = 'AIzaSyDjuqAztMNv_TgGdQMdpjMo68x9eNbEl-E')
address = 'Constitution Ave NW & 10th St NW, Washington, DC'
lat, lng = gmaps.address_to_latlng(address)
print lat, lng

错误信息如下:

C:\Users\Linfeng\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.1.0.1371.win-x86_64\lib\urllib2.pyc in http_error_default(self, req, fp, code, msg, hdrs)
    525 class HTTPDefaultErrorHandler(BaseHandler):
    526     def http_error_default(self, req, fp, code, msg, hdrs):
--> 527         raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    528 
    529 class HTTPRedirectHandler(BaseHandler):
HTTPError: HTTP Error 403: Forbidden

我已经在谷歌上打开了所有与地图有关的服务。

该软件包是否太旧,因此不受 Google 支持?

谢谢你的帮助!

一切顺利,

-临丰

4

3 回答 3

3

我遇到了和你一样的错误,但没有找到令人满意的答案,所以我按照这个网站上的这个方法更改了我的脚本来创建我需要的脚本(http://www.porttailsig.org/content/python- geocodage-geolocalisation -- 但是用法语写的)

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'])

它对我来说很好,除了有时我必须修复的索引错误。

于 2013-09-17T01:00:27.710 回答
1

您可以尝试在 python 中使用 geoPy 包。示例代码如下:

from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.geocode("Ratainda")
print((location.latitude, location.longitude))
于 2016-10-18T10:11:14.843 回答
0

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

于 2013-09-27T12:24:44.347 回答