B4J Question googlemap lat & long

ivanomonti

Expert
Licensed User
Longtime User
How can I find lat and long coordinates from the address only, using the google map library?
 

ivanomonti

Expert
Licensed User
Longtime User
I'm trying to insert a map using this code, everything is fine until it comes to the CameraPosition line, here it crashes giving me this error.
I can not understand the kind of error

B4X:
Sub google_geolocation(Place As String, lat As TextField, lon As TextField) As ResumableSub
    Dim res() As Double = Array As Double(0, 0)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download2("https://maps.googleapis.com/maps/api/geocode/json", Array As String("key", apykey, "address", Place))
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim jp As JSONParser
        jp.Initialize(j.GetString)
        Dim m As Map = jp.NextObject
        If m.Get("status") = "OK" Then
            Dim results As List = m.Get("results")
            If results.Size > 0 Then
                Dim first As Map = results.Get(0)
                Dim geometry As Map = first.Get("geometry")
                Dim location As Map = geometry.Get("location")
                res(0) = location.Get("lat")
                lat.Text = res(0)
                res(1) = location.Get("lng")
                lon.Text = res(1)
            End If
        End If
    Else
        Log("Error!")
    End If
    j.Release
    
    Dim googlemap As GoogleMap
    googlemap.Initialize2("",Null,apykey)
    googlemap.MapType = googlemap.MAP_TYPE_SATELLITE
    
    Dim cp As CameraPosition
    cp.Initialize(res(0),res(1), 18)
    googlemap.MoveCamera(cp)

    Return res
    
End Sub

B4X:
Waiting for debugger to connect...
Program started.
Alert: Hide directions called
Alert: loadMapLibrary done
Errore nella linea: 43
java.lang.NullPointerException
    at anywheresoftware.b4j.objects.GoogleMapWrapper.setMapType(GoogleMapWrapper.java:646)
    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.shell.Shell.runVoidMethod(Shell.java:657)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    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:497)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
    at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:19)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:135)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:84)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:482)
    at anywheresoftware.b4a.keywords.Common.access$0(Common.java:462)
    at anywheresoftware.b4a.keywords.Common$CallSubDelayedHelper.run(Common.java:536)
    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._enterNestedEventLoopImpl(Native Method)
    at com.sun.glass.ui.win.WinApplication._enterNestedEventLoop(WinApplication.java:218)
    at com.sun.glass.ui.Application.enterNestedEventLoop(Application.java:511)
    at com.sun.glass.ui.EventLoop.enter(EventLoop.java:107)
    at com.sun.javafx.tk.quantum.QuantumToolkit.enterNestedEventLoop(QuantumToolkit.java:583)
    at javafx.stage.Stage.showAndWait(Stage.java:474)
    at anywheresoftware.b4j.objects.Form.ShowAndWait(Form.java:215)
    at b4j.example.main._button_new_client_click(main.java:888)
    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.shell.Shell.runMethod(Shell.java:613)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:231)
    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:497)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
    at anywheresoftware.b4a.BA$1.run(BA.java:215)
    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)
 
Upvote 0
Top