Reputation: 386
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.
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.
Upvotes: 0
Views: 8061
Reputation: 763
Documentation :
http://code.google.com/apis/maps/documentation/directions/
You have to prepare url string using this template :
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 :
<?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>
Upvotes: 3
Reputation: 8439
To calculate the travel distance and travel time for multiple origins and destinations, use the Distance Matrix service in the Google Maps JavaScript API or Google Distance Matrix API (a web service)
Upvotes: 1