我正在使用谷歌地图api和这个python代码来打印两点之间的路线。
import requests, json
#Google MapsDdirections API endpoint
endpoint = 'https://maps.googleapis.com/maps/api/directions/json?'
api_key = 'AIzaSyCTPkufBttRcfSkA9zPYgivrYs9QEhdEEU'
#Asks the user to input Where they are and where they want to go.
origin = input('Where are you?: ').replace(' ','+')
destination = input('Where do you want to go?: ').replace(' ','+')
#Building the URL for the request
nav_request = 'origin={}&destination={}&key={}'.format(origin,destination,api_key)
request = endpoint + nav_request
#Sends the request and reads the response.
#response = urllib.request.urlopen(request).read()
r = requests.get('https://maps.googleapis.com/maps/api/directions/json?origin=Vigo&destination=Lugo&key=AIzaSyCTPkufBttRcfSkA9zPYgivrYs9QEhdEEU')
#Loads response as JSON
#directions = json.loads(response)
directions = r.json()
print(directions)
问题是我的回答给了我ZERO_RESULTS。 我已经在谷歌浏览器中手动尝试过,得到下一个结果:
{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "ChIJbYcwsYDOMQ0RDAVnKL9fMB8",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJk8GyYRRiLw0Rn9RLF60dRHs",
"types" : [ "locality", "political" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 43.0082848,
"lng" : -7.554997200000001
},
"southwest" : {
"lat" : 42.2392374,
"lng" : -8.720694999999999
}
},
"copyrights" : "Datos de mapas ©2019 Inst. Geogr. Nacional",
"legs" : [
{
"distance" : {
"text" : "188 km",
"value" : 188311
},
"duration" : {
"text" : "2h 11 min",
"value" : 7830
},
"end_address" : "Vigo, Pontevedra, España",
"end_location" : {
"lat" : 42.2406168,
"lng" : -8.720694999999999
},
"start_address" : "Lugo, España",
"start_location" : {
"lat" : 43.0082848,
[...]
但是,当我在线尝试时,我得到了不同的地理编码航点,因此zero_results。
'types': ['bar', 'establishment', 'food', 'point_of_interest', 'restaurant']}], 'routes': [], 'status': 'ZERO_RESULTS'}
如何更改geocoded_waypoint类型?
正如我从您的示例中所看到的,您尝试在两个西班牙城市之间获取方向。但是,当您仅指定起点和目的地中的城市名称时,由于参数不明确,服务可能会将它们解析为不同的国家/地区。例如,当我在位于美国的服务器上执行您的请求时,目的地 Lugo 被解析为放置 IDChIJi59iTw6wZIgRvssCUK9Ra84
,这是一家名为"Lugo's"的餐厅,位于 107 S Main St, Dickson, TN 37055, USA。
查看地点详细信息
https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJi59iTw6wZIgRvssCUK9Ra84&fields=formatted_address,geometry,name,type&key=YOUR_API_KEY
由于始发地位于西班牙,目的地位于美国,因此方向服务无法构建行车路线并返回ZERO_RESULTS。
为了解决歧义,您应该提供更精确的起点和终点参数,或指定要搜索结果的区域。
如果我在请求中添加region
参数,我会得到西班牙城市之间的预期路线
https://maps.googleapis.com/maps/api/directions/json?origin=Vigo&destination=Lugo®ion=ES&key=YOUR_API_KEY
您可以在方向计算器中看到它:
https://directionsdebug.firebaseapp.com/?origin=Vigo&destination=Lugo®ion=ES
我希望我的回答能澄清你的疑问!