Google 距离矩阵不包含村庄

Google distance matrix doesn't contain village

我编写了一个函数来获取包含 city/village 名称的输入。现在看来 Google API 方向矩阵无法识别给定的值之一。有谁知道如何获得结果?如果我在 Google 地图中输入村庄名称(荷兰的 Erp),它会毫不费力地找到它。

函数代码:

def calc_dist(dest):
    jsonReq = json.load(uReq(jsonURL + source + "&destinations=" + dest + 
    "&key=" + googleAPI))
    distance = jsonReq['rows'][0]['elements'][0]['distance']['value']
    return distance

错误是:

Traceback (most recent call last):
File "webscraper_tester.py", line 88, in <module>
distance = str(calc_dist(destination))
File "webscraper_tester.py", line 25, in calc_dist
distance = jsonReq['rows'][0]['elements'][0]['distance']['value']
KeyError: 'distance'

生成的URL如下:

https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=Nijmegen,GE&destinations=Erp,NB&key="API-KEY"

Google 地图在其各种 API 中使用 ISO 3166-1 国家代码,在查找地点时,最好包含国家代码。

荷兰的国家代码是NL。如果我重写您的请求,删除看起来像是您包含的区域代码并添加国家代码,如下所示:

https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=Nijmegen,NL&destinations=Erp,NL&key=YOUR_KEY

然后returns:

{
  "destination_addresses": [
    "5469 Erp, Netherlands"
  ],
  "origin_addresses": [
    "Nijmegen, Netherlands"
  ],
  "rows": [{
    "elements": [{
      "distance": {
        "text": "45.7 km",
        "value": 45652
      },
      "duration": {
        "text": "35 mins",
        "value": 2113
      },
      "status": "OK"
    }]
  }],
  "status": "OK"
}