Google地图API计算路线时间
Google Maps API to calculate route time
很抱歉,我还没有编写任何代码来分享,但我已经搜索了很长时间,但一直未能找到好的来源或解决方案。我想弄清楚我是否可以合并 google 地图 api 来计算两个位置之间的估计旅行时间,我可以 return 在 Windows表格。
起始位置和目的地位置将被输入到两个单独的文本框中,其结果是,当单击按钮时,或者一旦输入信息的表格被保存,估计的旅行时间将是计算并显示在网格或文本框中。我看过几篇介绍如何计算距离的文章,我考虑过通过计算距离并将其除以速度常数来尝试计算一个非常粗略的估计时间,但我真的很想知道我是否可以使用google 对此的应用。
要计算多个起点和终点的行程距离和行程时间,请使用 Distance Matrix service in the Google Maps JavaScript API or Google Distance Matrix API(网络服务)
文档:
http://code.google.com/apis/maps/documentation/directions/
您必须使用此模板准备 url 字符串:
http://maps.googleapis.com/maps/api/directions/xml?origin={0}&destination={1}&sensor={2}&language={3}{4}{5}{6}
然后执行请求:
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();
}
然后你得到 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>
很抱歉,我还没有编写任何代码来分享,但我已经搜索了很长时间,但一直未能找到好的来源或解决方案。我想弄清楚我是否可以合并 google 地图 api 来计算两个位置之间的估计旅行时间,我可以 return 在 Windows表格。
起始位置和目的地位置将被输入到两个单独的文本框中,其结果是,当单击按钮时,或者一旦输入信息的表格被保存,估计的旅行时间将是计算并显示在网格或文本框中。我看过几篇介绍如何计算距离的文章,我考虑过通过计算距离并将其除以速度常数来尝试计算一个非常粗略的估计时间,但我真的很想知道我是否可以使用google 对此的应用。
要计算多个起点和终点的行程距离和行程时间,请使用 Distance Matrix service in the Google Maps JavaScript API or Google Distance Matrix API(网络服务)
文档:
http://code.google.com/apis/maps/documentation/directions/
您必须使用此模板准备 url 字符串:
http://maps.googleapis.com/maps/api/directions/xml?origin={0}&destination={1}&sensor={2}&language={3}{4}{5}{6}
然后执行请求:
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();
}
然后你得到 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>