Sub updateDirections
Dim source, destination As String
Dim jo As JavaObject
Dim Points As List
Dim res As Int
Dim curloc As LatLng
curloc.Initialize(LastLoc.Latitude, LastLoc.Longitude)
Points.Initialize
jo.InitializeStatic("com.poly.PolyUtil")
Points = pline.Points
Dim tolerance As Double
tolerance = 10 '10 m
res = jo.RunMethod("locationIndexOnPath", Array(curloc, Points, False, tolerance)) 'checks whether the given point lies on or near a polyline, within a specified tolerance in meters
'returns -1 if point does not lie on or near the polyline.
' 0 if point is between poly[0] and poly[1] (inclusive),
' 1 if between poly[1] and poly[2],
' 2 if between poly[2] and poly[3],
' ...etc
Dim res1 As Double
Dim p1, p2 As LatLng
Dim pathdata As String
p1.Initialize(LastLoc.Latitude, LastLoc.Longitude)
pathdata = pathdata & "last loc " & LastLoc.Latitude & ", " & LastLoc.Longitude & CRLF
If res = -1 Then 'not on path
source = LastLoc.Latitude & "," & LastLoc.Longitude 'current location from GPS_LocationChanged sub
destination = DestLoc.Latitude & "," & DestLoc.Longitude 'destination location coordinates
p2 = Points.Get(0)
GetDirections(source, destination) 'get new directions
Else 'on path, res = index in Points list (polyline)
p2 = Points.Get(res)
If res = 0 Then '0 so it is the first item in the Points list (polyline)
Dim resll As LatLng
jo.InitializeStatic("com.poly.PolyUtil")
resll = jo.RunMethod("intersectOnLine", Array(curloc, Points.Get(0), Points.Get(1))) 'find coordinates on the polyline where current location intersects with polyline
If resll.Latitude <> 0 And resll.Longitude <> 0 Then 'resl1 equals to coordinates on the polyline, if current loc. intesects with polyline path
'res11 = 0,0 - current location doesn't intersect with the polyine,
Points.Set(0, resll)
pline.Points = Points
pline.Color = Colors.Red
End If
Else '>0 so, remove polyline points from the Points list and update polyline
If Points.Size > 2 Then
For i = 0 To res - 1
Points.RemoveAt(0)
Next
pline.Points = Points 'set polyline
pline.Color = Colors.Green
End If
End If
End If
End Sub