B4J Question Distance


Active Member
Licensed User
Longtime User
I dont know what is my error in my code.

Sub Process_Globals
    Dim gMaps As GoogleMap
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    gMaps.Initialize("", Null)
    Dim mLatC As Double = 53.63746167  
    Dim mLonC As Double = 9.799283333  
    Dim mLatP As Double = 53.63746333  
    Dim mLonP As Double = 9.799173333  
    Dim mDistance As Double

    mDistance = MapDistanceBetween(mLatC, mLonC, mLatP, mLonP)

End Sub

Sub MapDistanceBetween(mLatFrom As Double, mLonFrom As Double, mLatTo As Double, mLonTo As Double) As Double
  Dim jo As JavaObject
  jo.InitializeNewInstance("com.lynden.gmapsfx.javascript.object.LatLong", Array(mLatFrom, mLonFrom))
  Dim mLatLon As LatLng
  mLatLon.Initialize(mLatTo, mLonTo)
  Return jo.RunMethod("distanceFrom", Array(mLatLon))
End Sub

And heres the error...

Program started.
main._mapdistancebetween (java line: 79)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:90)
at b4j.example.main._mapdistancebetween(main.java:79)
at b4j.example.main._appstart(main.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.start(main.java:36)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: netscape.javascript.JSException: ReferenceError: Can't find variable: google
at com.sun.webkit.dom.JSObject.fwkMakeException(JSObject.java:128)
at com.sun.webkit.WebPage.twkExecuteScript(Native Method)
at com.sun.webkit.WebPage.executeScript(WebPage.java:1439)
at javafx.scene.web.WebEngine.executeScript(WebEngine.java:982)
at com.lynden.gmapsfx.javascript.JavaFxWebEngine.executeScript(JavaFxWebEngine.java:39)
at com.lynden.gmapsfx.javascript.JavascriptRuntime.execute(JavascriptRuntime.java:63)
at com.lynden.gmapsfx.javascript.JavascriptObject.<init>(JavascriptObject.java:76)
at com.lynden.gmapsfx.javascript.object.LatLong.<init>(LatLong.java:36)
... 23 more


Active Member
Licensed User
Longtime User
Ah. Tnx. I found a workaround to get the distance without using google map..

Sub AppStart (Form1 As Form, Args() As String)
    Log("Miles: " & Distance(14.656081, 121.032142, 14.7080424, 121.0375839, ""))
    Log("Kilometer: " & Distance(14.656081, 121.032142, 14.7080424, 121.0375839, "K"))
    Log("Nautical: " & Distance(14.656081, 121.032142, 14.7080424, 121.0375839, "N"))
End Sub

Sub Distance(Lat1 As Double, Lon1 As Double, Lat2 As Double, Lon2 As Double, Unit As String) As Double
    Dim theta As Double = Lon1 - Lon2
    Dim dista As Double = Sin(deg2rad(Lat1)) * Sin(deg2rad(Lat2)) + Cos(deg2rad(Lat1)) * Cos(deg2rad(Lat2)) * Cos(deg2rad(theta))
        dista = ACos(dista)
        dista = rad2deg(dista)
        dista = dista * 60 * 1.1515
        If Unit = "K" Then
            dista = dista * 1.609344
        Else If Unit = "N" Then
            dista = dista * 0.8684
        End If
        Return dista
End Sub

Sub deg2rad(d As Double) As Double
    Return d * cPI / 180.0
End Sub

Sub rad2deg(r As Double) As Double
    Return r * 180 / cPI
End Sub
Upvote 0


Active Member
Licensed User
Longtime User
@BarryW: you measure distance point to point? do you have example cide to measure distance with multipoint?

You can also used the code i posted.

Point1 = 14.123, 121.123
Point2 = 14.456, 121.456
Point2 = 14.789, 121.789

MultiDistance = Distance(14.123, 121.123, 14.456, 121.456, "K") + Distance(14.456, 121.456, 14.789, 121.789, "K")

Just continue to get distance from 1 point to another then add them.
Upvote 0