使用Python中的Google距离矩阵API计算两个地点之间的距离和持续时间?
我们几乎所有人都使用Google映射来检查源和目的地之间的距离并检查旅行时间。对于开发人员和发烧友,谷歌提供“谷歌距离矩阵API”来计算两个地方之间的距离和持续时间。
要使用Google距离矩阵API,我们需要GoogleMapsAPI键,您可以从下面的链接中获取:
https://developers.google.com/maps/documentation/distance-matrix/get-api-key
所需的库
我们可以通过使用不同的python库来完成此操作,例如:
大熊猫
谷歌映射
要求
杰森
我正在使用非常基本的请求和json库。使用熊猫,您可以一次填充多个源和目标位置,并将结果保存到csv文件中。
下面是实现相同的程序:
# Import required library
import requests
import json
#Enter your source and destination city
originPoint = input("Please enter your origin city: ")
destinationPoint= input("Please enter your destination city: ")
#Place your google map API_KEY to a variable
apiKey = 'YOUR_API_KEY'
#Store google maps api url in a variable
url = 'https://maps.googleapis.com/maps/api/distancematrix/json?'
# call get method of request module and store respose object
r = requests.get(url + 'origins = ' + originPoint + '&destinations = ' + destinationPoint + '&key = ' + apiKey)
#Get json format result from the above response object
res = r.json()
#print the value of res
print(res)输出结果
Please enter your origin city: Delhi
Please enter your destination city: Karnataka
{'destination_addresses': [‘Karnataka, India’],'origin_addresses': [‘Delhi, India’], 'rows': [{'elements': [{'distance': {'text': '1,942 km', 'value': 1941907}, 'duration': {'text': '1 day 9 hours', 'value': 120420},
'status': 'OK'}]}], 'status': 'OK'}热门推荐
1 训练狗狗搜寻
2 如何训练狗狗装死
3 狗狗训练书
6 怎么训练狗狗拜年
8 狗狗训练游戏
10 狗狗嗅觉训练
11 怎么训练狗狗在笼子里不叫
12 怎么训练狗狗站立走路
13 怎么训练狗狗在马桶上
14 狗狗尿尿怎么训练
15 训练狗狗用品
16 泰迪狗狗的训练方法大小便
17 训练狗狗可以打吗
18 王曼丽的狗狗训练