I think it can work on lat/lng coordinates. When I tried it on a single set (point X and line points A and B, all set by lat/lng types), using the posted code from TILogistic, then
it works and I get the right (shortest) distance from point X to line A_B.
However when I run this in a loop, looping through a recordset, holding lat and lng values, I get an error, which sofar I have been unable to track and explain:
p_map$ResumableSub_GetDistanceFromRoute3resume (java line: 8242)
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
at b4a.exampleljjll.p_map$ResumableSub_GetDistanceFromRoute3.resume(p_map.java:8242)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:275)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:215)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.keywords.Common$15.run(Common.java:1857)
at android.os.Handler.handleCallback(Handler.java:995)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loopOnce(Looper.java:273)
at android.os.Looper.loop(Looper.java:363)
at android.app.ActivityThread.main(ActivityThread.java:10060)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:632)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Starter, LogError, strErrorMsg: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
Code:
Sub GetDistanceFromRoute3(iIDX As Int, tLLX As TMapLatLng, tLLA As TMapLatLng, tLLB As TMapLatLng) As ResumableSub
Dim rs1 As ResumableSub = NearestPointOnline2(iIDX, tLLX, tLLA, tLLB)
Wait For (rs1) Complete (tLL_AB As TMapLatLng)
'SetMapLocation(tLL_AB)
'Log(iIDX & ": " & tLL_AB.fLat & "," & tLL_AB.fLng)
'Sleep(3000)
Dim rs2 As ResumableSub = MapUtilities.DistVincenty(tLL_AB, tLLX, "GetDistanceFromRoute3")
Wait For (rs2) Complete (dDistanceLX As Double)
' If iIDX = 34 Or iIDX = 35 Then
' Log(iIDX & ": " & iIDX & ", distance: " & dDistanceLX)
' End If
Return dDistanceLX
End Sub
Sub NearestPointOnline2(iIDX As Int, tLLX As TMapLatLng, tLLA As TMapLatLng, tLLB As TMapLatLng) As ResumableSub 'ignore
Dim tLL As TMapLatLng
Dim ABx As Double = tLLB.fLng - tLLA.fLng
Dim ABy As Double = tLLB.fLat - tLLA.fLat
Dim APx As Double = tLLX.fLng - tLLA.fLng
Dim APy As Double = tLLX.fLat - tLLA.fLat
Dim ab2 As Double = ABx * ABx + ABy * ABy
Dim ap_ab As Double = APx * ABx + APy * ABy
Dim t As Double = ap_ab / ab2
' If iIDX = 35 Or iIDX = 34 Then
' Log("iIDX: " & iIDX & ", t: " & t)
' End If
' Limit t to correspond to the segment, if you want the segment, not the infinite line
If t < 0 Then t = 0
If t > 1 Then t = 1
Dim cx As Double = tLLX.fLng + t * ABx
Dim cy As Double = tLLX.fLat + t * ABy
Dim cx As Double = tLLX.fLng + t * ABx
Dim cy As Double = tLLX.fLat + t * ABy
tLL.Initialize
tLL.fLng = cx
tLL.fLat = cy
'SetMapLocation(tLL)
'Sleep(3000)
Return tLL
End Sub
Also I get too short distances when the lines are not near the point X (but all less than 1500 meters).
Maybe it does indeed need the lat/lng scaling as you suggested.
Will investigate further.
RBS