Android Question Click Button to Get Latitude and longitude

Pravee7094

Active Member
Hi all,
I used the below code to get the latitude and longitude of current location and automatically zoom to the current location. It works perfectly.
B4X:
'starter
Sub GPS_LocationChanged (Location1 As Location)
    If Timer1.Enabled = True Then
        CallSub2(activity, "LocationChanged", Location1)
    End If
End Sub
B4X:
'activity
Sub LocationChanged(Location1 As Location)
    Log("Latitude : " & Location1.Latitude)
    Log("Longitude : " & Location1.Longitude)
    Dim cp As CameraPosition
    cp.Initialize(Location1.Latitude, Location1.Longitude, 17)
    ig_googlemap.AnimateCamera(cp)
End Sub

But I want to Click the one button (Get current location) and it get latitude and longitude values and automatically zoom to the current location. In here
B4X:
Sub ipbtn_currentlocaiton_Click

End Sub
I tried but does not find the solution

Anybody Help?

Thanks
praveen
 

roumei

Active Member
Licensed User
Getting a GPS location requires the device to get a satellite fix and that might take some time (up to 30 s). So tapping on a button to start the GPS and immediately zooming to a location will probably not work in most cases.
You could save the received location in a global variable in LocationChanged and zoom to that if the user clicks the button.
 
Upvote 0

Pravee7094

Active Member
Getting a GPS location requires the device to get a satellite fix and that might take some time (up to 30 s). So tapping on a button to start the GPS and immediately zooming to a location will probably not work in most cases.
You could save the received location in a global variable in LocationChanged and zoom to that if the user clicks the button.
Thanks for your suggestion. Solved
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
3 tips:
1. Why not get the location from Google Maps directly (gmap.MyLocation)?
2. Better to use FusedLocationProvider. It also has a method to get the last known location.
3. Switch to B4XPages. You will no longer need to pass information between the starter service and the activity (as well as many other things that will become simpler).
 
Upvote 0
Top