vb.net 用于计算路线时间的 Google Maps API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27945734/
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
Google Maps API to calculate route time
提问by user3841709
sorry in advance for not having any code written yet to share but I have been searching around for quite some time yet and haven't been able to find a good source or solution. I am trying to figure out if it is possible for me to incorporate a google maps api that will calculate an estimated travel time between two locations that I can return to the user in a Windows Form.
很抱歉没有编写任何代码尚未共享,但我已经搜索了相当长的时间并且无法找到好的来源或解决方案。我试图弄清楚我是否可以合并一个谷歌地图 API,该 API 将计算我可以在 Windows 窗体中返回给用户的两个位置之间的估计旅行时间。
The starting location and destination location will be entered into two separate textbox's the outcome of this is, either when a button is clicked, or once the form that the information is entered into is saved, an estimated travel time will be calculated and shown in a grid or textbox. I have seen a few articles that show how to calculate the distance and I have thought about trying to calculate a really rough estimated time by calculating the distance and dividing it by a constant for the speed but I would really like to know if I can use google's application of this instead.
起始位置和目的地位置将被输入到两个单独的文本框中,其结果是,无论是单击按钮时,还是保存输入信息的表单后,都会计算估计的旅行时间并将其显示在网格或文本框。我看过几篇文章,展示了如何计算距离,我曾考虑尝试通过计算距离并将其除以速度常数来计算一个非常粗略的估计时间,但我真的很想知道我是否可以使用谷歌对此的应用。
回答by Majkl
Documentation :
文件:
http://code.google.com/apis/maps/documentation/directions/
http://code.google.com/apis/maps/documentation/directions/
You have to prepare url string using this template :
您必须使用此模板准备 url 字符串:
http://maps.googleapis.com/maps/api/directions/xml?origin={0}&destination={1}&sensor={2}&language={3}{4}{5}{6}
http://maps.googleapis.com/maps/api/directions/xml?origin={0}&destination={1}&sensor={2}&language={3}{4}{5}{6}
Then perform request :
然后执行请求:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = requestAccept;
using(HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
using(Stream responseStream = response.GetResponseStream())
{
using(StreamReader read = new StreamReader(responseStream, Encoding.UTF8))
{
ret = read.ReadToEnd();
}
}
response.Close();
}
And in ret you get xml response :
在 ret 你得到 xml 响应:
<?xml version="1.0" encoding="UTF-8"?>
<DirectionsResponse>
<status>OK</status>
<route>
<summary>Cherry Hill Rd</summary>
<leg>
<step>
<travel_mode>DRIVING</travel_mode>
<start_location>
<lat>41.8542652</lat>
<lng>-74.1999310</lng>
</start_location>
<end_location>
<lat>41.8506910</lat>
<lng>-74.1990648</lng>
</end_location>
<polyline>
<points>etm~Fpd{cM\BjBVbB?x@DrBd@R?TCRK^YvA}AtAgAb@e@</points>
</polyline>
<duration>
<value>31</value>
<text>1 min</text>
</duration>
<html_instructions>Head <b>south</b> on <b>Cherry Hill Rd</b> toward <b>Hornbeck Rd</b></html_instructions>
<distance>
<value>432</value>
<text>0.3 mi</text>
</distance>
</step>
<duration>
<value>31</value>
<text>1 min</text>
</duration>
<distance>
<value>432</value>
<text>0.3 mi</text>
</distance>
<start_location>
<lat>41.8542652</lat>
<lng>-74.1999310</lng>
</start_location>
<end_location>
<lat>41.8506910</lat>
<lng>-74.1990648</lng>
</end_location>
<start_address>232 Cherry Hill Road, Accord, NY 12404, USA</start_address>
<end_address>22 Cherry Hill Road, Accord, NY 12404, USA</end_address>
</leg>
<copyrights>Map data ?2015 Google</copyrights>
<overview_polyline>
<points>etm~Fpd{cMhCZbB?x@DrBd@R?TCRK^YvA}AtAgAb@e@</points>
</overview_polyline>
<bounds>
<southwest>
<lat>41.8506910</lat>
<lng>-74.2002909</lng>
</southwest>
<northeast>
<lat>41.8542652</lat>
<lng>-74.1990648</lng>
</northeast>
</bounds>
</route>
</DirectionsResponse>
回答by Verma
To calculate the travel distance and travel time for multiple origins and destinations, use the Distance Matrix servicein the Google Maps JavaScript API or Google Distance Matrix API(a web service)
要计算多个起点和终点的行驶距离和行驶时间,使用距离矩阵服务的谷歌地图的JavaScript API或谷歌距离矩阵API(网络服务)

