• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Loop and extract public transit details from XML

Tom A

Member
Hi,

This question is to do with XML. I'm blind and would like to be able to access public transit information from google maps (https://developers.google.com/maps/documentation/directions/) in a simple format.
I am including a map with the directions as stated in the google maps terms and conditions.
I can get driving and walking directions fine using the below code apart from public transit info. I have code that loops through the steps of a journey and displays each step but I can't work out how to loop through the transit_details array at the same time and link it to the current step.

This is the kind of output I would like when displaying transit info:

"51 mins, 8.9 km
Walk to Oakwood Clock (S bound) - 11 mins - 0.9 km
Bus towards Middleton - 16 mins - 3.9 km
Departure time: 10:47
Arrival time: 11:03
Num of stops: 3
Walk to The Headrow (stop H10) - 2 mins - 0.1 km
Bus towards Moor Grange - 16 mins - 3.8 km
Departure time: 11:40
Arrival time: 11:50
Num of stops: 2
Walk to Cross St Michael's Lane, Leeds, West Yorkshire LS6, UK - 1 min - 0.1 km"


Code:
Function gglDirectionsResponse(ByVal strStartLocation, ByVal strEndLocation, ByRef strTravelTime, ByRef lngTravelTime, ByRef strDistance, ByRef lngDistance, ByRef strInstructions, Optional ByVal strTravelMode, Optional ByRef strError = "") As Boolean
On Error GoTo errorHandler
' Helper function to request and process XML generated by Google Maps.

Dim strURL As String
Dim objXMLHTTP As Object
Dim objDOMDocument As Object
Dim nodeRoute As Object

Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
Set objDOMDocument = CreateObject("MSXML2.DOMDocument.6.0")
 
strStartLocation = Replace(strStartLocation, " ", "+")
strEndLocation = Replace(strEndLocation, " ", "+")
 
  'Set the travel mode i.e. driving, walking
  If strTravelMode = "" Then
strTravelMode = "Driving"
  End If
 
strURL = "http://maps.googleapis.com/maps/api/directions/xml" & _
  "?origin=" & strStartLocation & _
  "&destination=" & strEndLocation & _
"&mode=" & strTravelMode & "&Region=GB&units=" & strUnits


'Send XML request
With objXMLHTTP
  .Open "GET", strURL, False
  .setRequestHeader "Content-Type", "application/x-www-form-URLEncoded"
  .Send
  objDOMDocument.LoadXML .responseText
End With
 
With objDOMDocument
  If .SelectSingleNode("//status").Text = "OK" Then
  'Get Distance
  lngDistance = .SelectSingleNode("/DirectionsResponse/route/leg/distance/value").Text ' Retrieves distance in meters
  Select Case strUnits
  Case "imperial": lngDistance = Round(lngDistance * 0.00062137, 1)  'Convert meters to miles
  Case "metric": lngDistance = Round(lngDistance / 1000, 1) 'Convert meters to kilometres
  End Select
 
  'Get distance in human friendly form
  strDistance = .SelectSingleNode("/DirectionsResponse/route/leg/distance/text").Text

  'Get Travel Time
  lngTravelTime = .SelectSingleNode("/DirectionsResponse/route/leg/duration/value").Text  'returns in seconds from google
  lngTravelTime = formatGoogleTime(lngTravelTime) 'converts seconds to hh:mm
 
'Travel time in human readable form i.e. autmatically adds the word mins at end
  strTravelTime = .SelectSingleNode("/DirectionsResponse/route/leg/duration/text").Text
 
  'Get Directions
Dim ixnNode As IXMLDOMNode

  For Each nodeRoute In .SelectSingleNode("//route/leg").ChildNodes
  If nodeRoute.BaseName = "step" Then
  strInstructions = strInstructions & nodeRoute.SelectSingleNode("html_instructions").Text & " - " & nodeRoute.SelectSingleNode("duration/text").Text & " - " & nodeRoute.SelectSingleNode("distance/text").Text & vbCrLf


'***public transport
'This code doesn't work correctly
Set ixnNode = nodeRoute.SelectSingleNode("//transit_details/departure_time/text")
s = GetTextValueFromSingleNode(ixnNode)

'Public transit details are available
If s <> "" Then
strInstructions = strInstructions & vbCrLf & "Departure time: " & s & vbCrLf
Set ixnNode = nodeRoute.SelectSingleNode("//transit_details/arrival_time/text")
s = GetTextValueFromSingleNode(ixnNode)
strInstructions = strInstructions & vbCrLf & "Arrival time: " & s & vbCrLf

Set nodes = nodeRoute.SelectNodes("//transit_details")
For Each node In nodes
  s = node.nodeName & " " & node.Text & vbCrLf
'MsgBox s
  Next node
End If

End If

  Next nodeRoute
 
'output
Dim strRouteSummary As String
'Get the route summary which contains a short textual description for the route, suitable for naming and disambiguating the route from alternatives.
'adding this along with the time and distance to the strInstructions variable
  strRouteSummary = .SelectSingleNode("/DirectionsResponse/route/summary").Text
  If strRouteSummary <> "" Then
  strRouteSummary = "via " & strRouteSummary
  End If
 
  strInstructions = strTravelTime & ", " & strDistance & " " & strRouteSummary & vbCrLf & CleanHTML(strInstructions)  'Removes MetaTag information from HTML result to convert to plain text.
 
  Else
  strError = .SelectSingleNode("//status").Text
  GoTo errorHandler
  End If
End With
 
gglDirectionsResponse = True
GoTo CleanExit
 
errorHandler:
  If strError = "" Then strError = Err.Description
  strDistance = -1
  lngDistance = -1
  strTravelTime = "00:00"
  lngTravelTime = 0
  strInstructions = ""
  gglDirectionsResponse = False
 
CleanExit:
  Set objDOMDocument = Nothing
  Set objXMLHTTP = Nothing
 
End Function

Thank you,
Tom
 
Last edited by a moderator:
Back
Top