Python 谷歌地图驾驶时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17267807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:55:05  来源:igfitidea点击:

Python Google Maps Driving Time

pythongoogle-mapsgoogle-maps-api-3

提问by tsspires

I need to get driving time between two sets of coordinates using Python. The only wrappers for the Google Maps API I have been able to find either use Google Maps API V2 (deprecated) or do not have the functionality to provide driving time. I'm using this in a local application and do not want to be bound to using JavaScript which is what the Google Maps API V3 is available in.

我需要使用 Python 获取两组坐标之间的行驶时间。我能找到的唯一的 Google Maps API 包装器要么使用 Google Maps API V2(已弃用),要么没有提供驾驶时间的功能。我在本地应用程序中使用它,并且不想被绑定到使用 JavaScript,这是 Google Maps API V3 可用的。

采纳答案by tsspires

Using URL requests to the Google Distance Matrix API and a json interpreter you can do this:

使用对 Google 距离矩阵 API 的 URL 请求和 json 解释器,您可以执行以下操作:

import simplejson, urllib
orig_coord = orig_lat, orig_lng
dest_coord = dest_lat, dest_lng
url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins={0}&destinations={1}&mode=driving&language=en-EN&sensor=false".format(str(orig_coord),str(dest_coord))
result= simplejson.load(urllib.urlopen(url))
driving_time = result['rows'][0]['elements'][0]['duration']['value']

回答by Chad tialino

Check out this link: https://developers.google.com/maps/documentation/distancematrix/#unit_systems

查看此链接:https: //developers.google.com/maps/documentation/distancematrix/#unit_systems

Read the part about "Optional parameters." essentially, you add the parameter into your request in your url. So if you wanted biking, it would be "mode=bicycling." Check out the example towards the bottom of the link and play around with some parameters. Good Luck!

阅读“可选参数”部分。本质上,您将参数添加到您的 url 请求中。所以如果你想骑自行车,那就是“模式=骑自行车”。查看链接底部的示例并使用一些参数。祝你好运!

回答by Domnick

import googlemaps
from datetime import datetime

gmaps = googlemaps.Client(key='YOUR KEY')


now = datetime.now()
directions_result = gmaps.directions("18.997739, 72.841280",
                                     "18.880253, 72.945137",
                                     mode="driving",
                                     avoid="ferries",
                                     departure_time=now
                                    )

print(directions_result[0]['legs'][0]['distance']['text'])
print(directions_result[0]['legs'][0]['duration']['text'])

This is been taken from hereAnd alternatively you can change the parameters accordingly.

这是从这里获取的,或者您可以相应地更改参数。