To Get Directions between two locations

We can get various details on the directions between two locations as XML like, total distance, total time taken by using various travel modes like(driving, cycling and walking etc.), all the way points and details of how to get there with directions. The code below explains how to send the web request and receive a XML response using google maps API. From this response XML we can get the direction details, total distance, total time taken by various travel modes, longitude and latitude for both the locations etc.

This response XML received may have various result status like :-

Private Function GetResponseXMLByFromAndToLocation(ByVal fromLocation As String, ByVal toLocation As String) As String

Dim responseXml As String = String.Empty

Dim webUrlForRequest As string
 

'construct the URL with concatenating the FROM and TO locations and send the web request to google maps webserver

Dim url As String = "http://maps.googleapis.com/maps/api/directions/xml"

Dim restUrl As String = "?origin=###FROMLOCATION###&destination=###TOLOCATION###&sensor=false"

restUrl = restUrl.Replace("###FROMLOCATION###", fromLocation.Replace(" ", "+"))

restUrl = restUrl.Replace("###TOLOCATION###", toLocation.Replace(" ", "+"))

webUrlForRequest = url & restUrl
 

'get the response for the request sent to the google maps webserver

Dim wsClient As New WebClient

responseXml = wsClient.DownloadString(webUrlForRequest)

Return responseXml

End Function

    OK – valid result.

    NOT_FOUND - at least one of the locations specified in the requests's origin, destination, or waypoints could not be geocoded.

    ZERO_RESULTS - no route could be found.

    INVALID_REQUEST - provided request was invalid.

    REQUEST_DENIED - service denied use of the directions service by your application.

    UNKNOWN_ERROR - directions request could not be processed due to a server error.



If _responseXml.Contains("OK") Then


Dim xmlDoc As New XmlDocument()

xmlDoc.LoadXml(_responseXml)

Dim stepsNodeList As XmlNodeList

Dim durationNodeList As XmlNodeList

Dim startLocationNodeList As XmlNodeList

Dim endLocationNodeList As XmlNodeList

stepsNodeList = xmlDoc.SelectNodes("/DirectionsResponse/route/leg/step/html_instructions")

durationNodeList = xmlDoc.SelectNodes("/DirectionsResponse/route/leg/step/duration/text")

startLocationNodeList = xmlDoc.SelectNodes("/DirectionsResponse/route/leg/start_address")

endLocationNodeList = xmlDoc.SelectNodes("/DirectionsResponse/route/leg/end_address")


----------- --------------

---------- ----------------------

-- ---XXXXXXX ---- XXXXX------- XXXXXXXX ----------


EndIf

Like this we can get the nodelists for each section of the response and we can proceed for getting the results we require from their inner texts.

This TIP is created by referring from the Google’s geocode site so any one wants to get an extra knowledge on using google’s maps API for getting the direction, distance or any other data among the two locations please go through the link :-

http://code.google.com/apis/maps/documentation/directions/

150 150 Burnignorance | Where Minds Meet And Sparks Fly!