Android Question Problems with Google Maps

zingo

Member
Licensed User
Longtime User
I migrated from version 6.50 to 10.2
I'm now testing new Google Maps API.
I've got this problem:

I always get an error in the log window:
Object should first be intialized
when I try to animate camera:

It happens when I try to use gmpa.MyLocation.Latitude for the first time
and even calling Cpos.Initialize

Testing AnimateCamera to zoom to position:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    Wait For MapFragment1_Ready   
    gmap = MapFragment1.GetMap
    gmap.MapType=1
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then               
        gmap.MyLocationEnabled = True

        Do While gmap.IsInitialized = False
            Log("...")
        Loop   

        Log("Pre LAT --> " & gmap.MyLocation.Latitude)
        Log("Pre LON --> " & gmap.MyLocation.Longitude)
        
        Dim Cpos As CameraPosition
            
        Cpos.Initialize(gmap.MyLocation.Latitude,gmap.MyLocation.Longitude, 17)
        Log("LAT --> " & gmap.MyLocation.Latitude)
        Log("LON --> " & gmap.MyLocation.Longitude)
        
        Do While Cpos.IsInitialized = False
            Log("2...")
        Loop
                
        gmap.AnimateCamera(Cpos)

    Else
        Log("Autorizzazione negata!")
    End If
End Sub

Anyway, despite of the error, stepping in debug works, the position is correct and CameraAnimate works.
If I press F10 in the IDE, the program crashes as it does in release mode.
I tried to test for initialization with no success.

Any suggestions?
 

DonManfred

Expert
Licensed User
Longtime User
It happens when I try to use gmpa.MyLocation.Latitude for the first time
The location is not known at this time. You need to wait for the position is avilable.

B4X:
    Do While gmap.MyLocation.IsInitialized = False
        Log("...")
        Sleep(100)
    Loop

    Dim myloc As LatLng = gmap.MyLocation ' Get the (kn)own loacation....
    If myloc.IsInitialized Then
        Log($"GMAP MyLocation: ${gmap.MyLocation}"$)
        Log("My Location available")


        Dim cp As CameraPosition
        cp.Initialize(myloc.Latitude, myloc.Longitude, 1) ' Use own location LAT LON
        gmap.AnimateCamera(cp)
    End If
 
Upvote 0

zingo

Member
Licensed User
Longtime User
Thanx.
Your solution is much better than mine, but i get the same problem.
Stepping through the debug works, with the right position.
Pressing F10 in the IDE crashes the app.
Same in release mode.
I even tried putting more sleep around (after each call, even up to 5000 milliseconds), but no way.
I must be missing something.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Tips;

Use Library:
GoogleMapsExtras: https://www.b4x.com/android/forum/threads/googlemapsextras.26277/
GPS

Note
:
you must activate the device's GPS

B4X:
Private GoogleMap1 As GoogleMap
Private GoogleMapsExtras1 As GoogleMapsExtras
Public LastLocation As Location

B4X:
    Wait For gmap_Ready
    GoogleMap1 = Mapfragment1.GetMap
    If GoogleMap1.IsInitialized Then
        rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
        Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
        If Result Then
            GoogleMap1.MyLocationEnabled = True
            GoogleMap1.GetUiSettings.MyLocationButtonEnabled = True
            
            Dim OnMyLocationChangeListener1 As OnMyLocationChangeListener
            OnMyLocationChangeListener1.Initialize("OnMyLocationChangeListener1")
            GoogleMapsExtras1.SetOnMyLocationChangeListener(GoogleMap1, OnMyLocationChangeListener1)
            Wait For OnMyLocationChangeListener1_MyLocationChange(Location1 As Location)
            
            LastLocation = Location1

            Dim CameraPosition1 As CameraPosition = GoogleMap1.CameraPosition
            Dim NewCameraPosition As CameraPosition
            NewCameraPosition.Initialize2(LastLocation.Latitude, LastLocation.Longitude, 15, CameraPosition1.Bearing, CameraPosition1.Tilt)
            GoogleMap1.AnimateCamera(NewCameraPosition)
            
        End If
       Return GoogleMap1.IsInitialized    
    End If

OPTIONAL:
If you want to track the location of your device
B4X:
Private Sub OnMyLocationChangeListener1_MyLocationChange(location1 As Location)
    Log("Position changed....")
    LastLocation = location1
    Log(LastLocation.Latitude & "," & LastLocation.Longitude)
End Sub
 
Upvote 0

zingo

Member
Licensed User
Longtime User
Thnx Oparra,
I'm reading documentation about the library you suggested, which is a completely different approach.
Very interesting, lot of nice implementation, I have to study.
Anyway, I launched the SDK manager to update everything to the last version, because I don't want to abandon the other solution yet and I can't find any obvious solution apart from a wrong or missing update.
Cheers.
 
Upvote 0
Top