B4J Question LatLng - distanceFrom

gravel

Member
Licensed User
Longtime User
I'm trying to use the method distanceFrom (and also getBearing) with the GoogleMaps library.

http://rterp.github.io/GMapsFX/apidocs/com/lynden/gmapsfx/javascript/object/LatLong.html

My app using OSMDroid needs to find the distance and bearing of the nearest Geopoint to each of the
objects it deals with.

It seemed sensible to precompute this on the server to be included in the download that the app requests every minute.

This code:
B4X:
  Dim NearPoint As LatLng
   Dim NearPointLat As Double = 52.750137
   Dim NearPointLon As Double = 1.072345
   Dim FarPoint As LatLng
   Dim FarPointLat As Double = 52.707931
   Dim FarPointLon As Double = 1.550293   
   Dim Here As JavaObject   
   NearPoint.Initialize(NearPointLat, NearPointLon)   
   Here.InitializeNewInstance("com.lynden.gmapsfx.javascript.object.LatLong", Array (NearPoint.Latitude, NearPoint.Longitude))   
   FarPoint.Initialize(FarPointLat, FarPointLon)
   Log(Here.RunMethod("distanceFrom", Array (FarPoint)))

generates this error on - NearPoint.Initialize(NearPointLat, NearPointLon)
Waiting for debugger to connect...
Program started.
Error occurred on line: 24 (Main)
java.lang.NullPointerException
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)
at anywheresoftware.b4j.objects.GoogleMapWrapper$LatLngWrapper.Initialize(GoogleMapWrapper.java:271)
at b4j.example.main._appstart(main.java:106)
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:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:612)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:229)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
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:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.start(main.java:36)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

If I remove the NearPoint and NearPoint.Initialize and change Here.InitializeNewInstance to

Here.InitializeNewInstance("com.lynden.gmapsfx.javascript.object.LatLong", Array (NearPointLat, NearPointLon))

I get this error on the Here.InitializeNewInstance
Waiting for debugger to connect...
Program started.
Error occurred on line: 26 (Main)
java.lang.reflect.InvocationTargetException
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:423)
at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:90)
at b4j.example.main._appstart(main.java:106)
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:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:612)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:229)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
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:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.start(main.java:36)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
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)
... 30 more
I'd be grateful for some pointer as to where I'm going wrong here.
 

gravel

Member
Licensed User
Longtime User
Do you mean where in the project? To try and get it to work I have hooked it out to fiddle with. That code comes from a test project.
B4X:
#Region  Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 400
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Button1 As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show

End Sub

Sub Button1_MouseClicked (EventData As MouseEvent)
   
    Dim NearPoint As LatLng
    Dim NearPointLat As Double = 52.750137
    Dim NearPointLon As Double = 1.072345
    Dim FarPoint As LatLng
    Dim FarPointLat As Double = 52.707931
    Dim FarPointLon As Double = 1.550293   
    Dim Here As JavaObject   
    NearPoint.Initialize(NearPointLat, NearPointLon)   
    Here.InitializeNewInstance("com.lynden.gmapsfx.javascript.object.LatLong", Array (NearPoint.Latitude, NearPoint.Longitude))   
    FarPoint.Initialize(FarPointLat, FarPointLon)
    Log(Here.RunMethod("distanceFrom", Array (FarPoint)))   
   
End Sub
 
Upvote 0
Top