Android Question How do I zoom in automatically to the current location in Google Maps

Cnrez

Member
Licensed User
Longtime User
hi,
when my application open, it show a google map, with 'my location' button, but user have to press 'my location' button to move the map.
question, How do I zoom in automatically to the current location in Google Maps when loading a google map ?

i try, but lat and long always 0

B4X:
Sub Activity_Create(FirstTime As Boolean)


    Wait For MapFragment1_Ready
    gmap = MapFragment1.GetMap
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
    
        Log(Tracker.gpslat)
        Log(Tracker.gpslong)
        
        gmap.MyLocationEnabled = True
        gmap.MyLocation.Initialize(Tracker.gpslat,Tracker.gpslong)
        longlat = gmap.MyLocation
        
        If gmap.MyLocation.IsInitialized Then
            Dim CameraPosition1 As CameraPosition            'CameraPosition1.Initialize2(gmap.MyLocation.Latitude, gmap.MyLocation.Longitude, gmap.CameraPosition.Zoom, 0, 0)
            CameraPosition1.Initialize(gmap.MyLocation.Latitude,gmap.MyLocation.Longitude, 17)           
            gmap.moveCamera(CameraPosition1)
        End If
        
    Else
        Log("No permission!")
    End If

End Sub

i also use https://www.b4x.com/android/forum/threads/background-location-tracking.99873/

thank you for your help
 

DonManfred

Expert
Licensed User
Longtime User
Sub Activity_Create(FirstTime As Boolean) Wait For MapFragment1_Ready
I dont think this will work!

You are NOT loading the layout with the Google map. Why?

In fact you are waiting for the MapFragment1_Ready event get raised but it will never happen as you are not loading the layout with the map which, in result, raises this event.

Check the GoogleMap Tutorial again and adapt your code

 
Last edited:
Upvote 0

Cnrez

Member
Licensed User
Longtime User
I dont think this will work!

You are NOT loading the layout with the Google map. Why?

In fact you are waiting for the MapFragment1_Ready event get raised but it will never happen as you are not loading the layout with the map which, in result, raises this event.

Check the GoogleMap Tutorial again and adapt your code


i am very sorry for that, the code above is not complete.

here is a complete project that i want to test
pls help

regards
 

Attachments

  • MyLocation.zip
    20.5 KB · Views: 246
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
basially i added this

B4X:
Sub Activity_Create(FirstTime As Boolean)
   
    Activity.LoadLayout("1") 'layout basic template
    Wait For MapFragment1_Ready
    gmap = MapFragment1.GetMap
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
   
        Log(Tracker.gpslat)  ' The tracker may not have a valid location at this time
        Log(Tracker.gpslong) ' Same here
   
        gmap.MyLocationEnabled = True ' You now enabled to use the MyLocation
       
        ' You should NOT initialize it by yourself
        'gmap.MyLocation.Initialize(Tracker.gpslat,Tracker.gpslong)
   
        ' Wait for the MyLocation is known....
        Do While gmap.MyLocation.IsInitialized = False
            Log("Location not initialized... Sleep a while...")
            Sleep(250)
        Loop
        If gmap.MyLocation.IsInitialized Then
            Dim CameraPosition1 As CameraPosition            'CameraPosition1.Initialize2(gmap.MyLocation.Latitude, gmap.MyLocation.Longitude, gmap.CameraPosition.Zoom, 0, 0)
            CameraPosition1.Initialize(gmap.MyLocation.Latitude,gmap.MyLocation.Longitude, 17)
            gmap.moveCamera(CameraPosition1)
        End If
       
    Else
        Log("No permission!")
    End If
End Sub

' This Sub gets called by the Tracker Service when a Location changes (GPS)
' See Tracker Service for details
Sub Update_Location(newloc As Location)
    If newloc.IsInitialized Then
        Dim CameraPosition1 As CameraPosition            'CameraPosition1.Initialize2(gmap.MyLocation.Latitude, gmap.MyLocation.Longitude, gmap.CameraPosition.Zoom, 0, 0)
        CameraPosition1.Initialize(newloc.Latitude,newloc.Longitude, 17)
        gmap.moveCamera(CameraPosition1)
    End If
End Sub

And also changed the Tracker Service a bit

B4X:
Sub GPS_LocationChanged (Location1 As Location)
    If DateTime.Now > LastUpdateTime + 10 * DateTime.TicksPerSecond Then
        Dim n As Notification = CreateNotification($"$2.5{Location1.Latitude} / $2.5{Location1.Longitude}"$)
        n.Notify(nid)
        LastUpdateTime = DateTime.Now
        gpslat = Location1.Latitude
        gpslong = Location1.Longitude
        CallSubDelayed2(Main,"Update_Location",Location1)
    End If
End Sub

Note that you need to use YOUR apikey and your packagename (i replaced them with mine)

Also note that you do not need to use MyLocation if you want to use the GPS Location given by Tracker Service.
 

Attachments

  • MyLocation.zip
    20.6 KB · Views: 346
Upvote 0

Cnrez

Member
Licensed User
Longtime User
thank you Don for your help and explanation, i run the project, and it works !
Now I understand.

regards
 
Upvote 0
Top