' Class module represents a DirectionsResult.DirectionsRoute.DirectionsLeg
' https://developers.google.com/maps/documentation/javascript/reference#DirectionsResult
Sub Class_Globals
' to do: check that defining a Type in a Class is ok
Type DirectionsStepType(Distance As DistanceType, Duration As DurationType, EndLocation As LatLngType, Instructions As String, _
Path() As LatLngType, StartLocation As LatLngType, TravelMode As String)
Type DistanceType(Text As String, Value As Int)
Type DurationType(Text As String, Value As Int)
Type LatLngType(Latitude As Double, Longitude As Double)
Public Distance As DistanceType
Public Duration As DurationType
Public EndAddress As String
Public EndLocation As LatLngType
Public StartAddress As String
Public StartLocation As LatLngType
Public Steps() As DirectionsStepType
End Sub
' Initializes the DirectionsLeg object. Parsing the JSONText to class properties.
Public Sub Initialize(JSONText As String)
Dim TempList As List
Dim TempMap, TempMap2 As Map
Dim Parser As JSONParser
Parser.Initialize(JSONText)
Dim JSONObject As Map
JSONObject=Parser.NextObject
TempMap=JSONObject.Get("distance")
Distance.Initialize
Distance.Text=TempMap.Get("text")
Distance.Value=TempMap.Get("value")
TempMap=JSONObject.Get("duration")
Duration.Initialize
Duration.Text=TempMap.Get("text")
Duration.Value=TempMap.Get("value")
EndAddress=JSONObject.Get("end_address")
TempMap=JSONObject.Get("end_location")
EndLocation.Initialize
EndLocation.Latitude=TempMap.GetValueAt(0)
EndLocation.Longitude=TempMap.GetValueAt(1)
StartAddress=JSONObject.Get("start_address")
TempMap=JSONObject.Get("start_location")
StartLocation.Initialize
StartLocation.Latitude=TempMap.GetValueAt(0)
StartLocation.Longitude=TempMap.GetValueAt(1)
TempList=JSONObject.Get("steps") ' TempList is now an Array of javascript DirectionStep objects
Dim i, StepsCount As Int
StepsCount=TempList.Size
Dim Steps(StepsCount) As DirectionsStepType
For i=0 To StepsCount-1
Steps(i).Initialize ' after initializing Steps(i) do we still need to initialize the child properties such as Distance?
TempMap=TempList.Get(i) ' TempMap is now a javascript DirectionsStep object
TempMap2=TempMap.Get("distance")
' Steps(i).Distance.Initialize
Steps(i).Distance.Text=TempMap2.Get("text")
Steps(i).Distance.Value=TempMap2.Get("value")
TempMap2=TempMap.Get("duration")
' Steps(i).Duration.Initialize
Steps(i).Duration.Text=TempMap2.Get("text")
Steps(i).Duration.Value=TempMap2.Get("value")
TempMap2=TempMap.Get("end_location")
' Steps(i).EndLocation.Initialize
Steps(i).EndLocation.Latitude=TempMap2.GetValueAt(0)
Steps(i).EndLocation.Longitude=TempMap2.GetValueAt(1)
Steps(i).Instructions=TempMap.Get("instructions")
' Steps(i).Path ' not parsed as it may not be required, it's an Array of LatLng coords of this part of the route
TempMap2=TempMap.Get("start_location")
' Steps(i).EndLocation.Initialize
Steps(i).StartLocation.Latitude=TempMap2.GetValueAt(0)
Steps(i).StartLocation.Longitude=TempMap2.GetValueAt(1)
Steps(i).TravelMode=TempMap.Get("travel_mode")
Next
End Sub