我计划在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支持?
谢谢你的帮忙!
万事如意,
-Linfeng
发布于 2013-09-17 09:00:28
我遇到了和你一样的错误,没有找到令人满意的答案,所以我按照这个网站上找到的方法修改了我的脚本,创建了我需要的脚本(http://www.portailsig.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'])它对我来说工作得很好,除了有时我必须修复索引错误。
发布于 2016-10-18 18:11:15
您可以尝试在python中使用geoPy包。示例代码如下:
from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.geocode("Ratainda")
print((location.latitude, location.longitude))发布于 2013-09-27 20:24:44
v2接口已于2013年9月13日关闭。你的问题是在14号提出的--所以可能是一个线索:)。我所做的就是简单地修改googlemaps.py中的URL,现在它就可以正常工作了。
我将_DIRECTIONS_QUERY_URL (googlemaps.py的第165行)改为:
_DIRECTIONS_QUERY_URL = 'http://maps.googleapis.com/maps/api/directions/output?'它运行得很好。我还试着按照建议的here修改了第164行:_GEOCODE_QUERY_URL = 'http://maps.googleapis.com/maps/api/geocode/output?',但有一些错误,并且a)无论如何我都不需要它b)它已经被pygeocoder覆盖了,它比googlemaps.py更高级一点
https://stackoverflow.com/questions/18807114
复制相似问题