python - Google距离矩阵不包含村庄

标签 python google-maps google-maps-api-3 directions google-distancematrix-api

我编写了一个函数,它获取包含城市/村庄名称的输入。现在看来 Google API 方向矩阵无法识别给定的值之一。有谁知道如何获得结果吗?如果我在 Google map 中输入村庄名称(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 map 在其各种 API 中使用 ISO 3166-1 国家/地区代码,在查找地点时,最好包含国家/地区代码。

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

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

然后返回:

{
  "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"
}

关于python - Google距离矩阵不包含村庄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56762769/

相关文章:

angular - 如何使用 Angular agm 来使用谷歌地图热图功能

python - 如何在Pyinstaller中包含Google API JSON凭证文件?

javascript - iPhone 的 Python 到 JavaScript

google-maps - Google Maps Java API V3 中的语言代码

android - 向谷歌地图添加大数据层 - Android

javascript - 如何从 GWT 代码添加 google maps 对象?

python - 在 macOS Sierra 上安装 pip

python - Matplotlib x 轴位于顶部,没有 x 轴标签,没有主要刻度标签,但需要外部主要和次要刻度

Android:获取当前位置后禁用 onLocationChanged()

javascript - 给定 LatLng 位置和方位 Angular 计算多边形边缘的位置