Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从谷歌云地图方向API获取距离_Python_Google Maps_Google Directions Api - Fatal编程技术网

Python 从谷歌云地图方向API获取距离

Python 从谷歌云地图方向API获取距离,python,google-maps,google-directions-api,Python,Google Maps,Google Directions Api,我正在试着计算公共汽车站之间的距离,以粗略估计公共汽车什么时候来。我知道有些应用和服务可以做到这一点,但我想自己去做 我得到了结果对象,但不知道如何获得距离 (代码来自。) 导入谷歌地图 从日期时间导入日期时间 #GC地图方向API coords_0='43.70721,-79.3955999' coords_1='43.7077599,-79.39294' gmaps=googlemaps.Client(key=api\u key) #通过公共交通请求指示 now=datetime.now()

我正在试着计算公共汽车站之间的距离,以粗略估计公共汽车什么时候来。我知道有些应用和服务可以做到这一点,但我想自己去做

我得到了结果对象,但不知道如何获得距离

(代码来自。)

导入谷歌地图
从日期时间导入日期时间
#GC地图方向API
coords_0='43.70721,-79.3955999'
coords_1='43.7077599,-79.39294'
gmaps=googlemaps.Client(key=api\u key)
#通过公共交通请求指示
now=datetime.now()
方向\u结果=GMAP。方向(协调0,协调1,模式=“驾驶”,出发时间=现在,避免使用“过路费”)
距离=方向\结果。

提前谢谢。

我已经运行了您的代码,并且
方向\u结果的长度为1。要获取总距离,请尝试以下代码:

# Import libraries
import googlemaps
from datetime import datetime
# Set coords
coords_0 = '43.70721,-79.3955999'
coords_1 = '43.7077599,-79.39294'
# Init client
gmaps = googlemaps.Client(key="KEY")
# Request directions
now = datetime.now()
directions_result = gmaps.directions(coords_0, coords_1, mode="driving", departure_time=now, avoid='tolls')

# Get distance
distance = 0
legs = directions_result[0].get("legs")
for leg in legs:
    distance = distance + leg.get("distance").get("value")
print(distance) # 222 i.e. 0.2 km

希望这对你有帮助

顺便说一句,我从示例代码中删除了地址地理编码部分。如果你想了解这背后的数学原理,并且你可以阅读JavaScript,那么这篇文章可能会对你有所帮助:@John Hanley我不是想了解数学原理,而是通过Python客户端获得距离的方法。另外,我要的是行驶距离,而不是直线距离。对不起,我把你的问题解释为试图复制
gmaps.directions
API的功能。您的示例不完整,因此我不知道为什么结果是空数组。编辑您的问题并包含真实代码。阅读本文,了解如何发布涉及编程的问题。后期编辑。添加了在谷歌地图(webapp)上测试的导入语句和真实坐标。
# Import libraries
import googlemaps
from datetime import datetime
# Set coords
coords_0 = '43.70721,-79.3955999'
coords_1 = '43.7077599,-79.39294'
# Init client
gmaps = googlemaps.Client(key="KEY")
# Request directions
now = datetime.now()
directions_result = gmaps.directions(coords_0, coords_1, mode="driving", departure_time=now, avoid='tolls')

# Get distance
distance = 0
legs = directions_result[0].get("legs")
for leg in legs:
    distance = distance + leg.get("distance").get("value")
print(distance) # 222 i.e. 0.2 km