Android Question GPS Question



I've been reading the GPS tutorials around the forums, but my use case is somewhat different. Erel's excellent example tutorial starts GPS as a Starter service and the GPS position updates in real time. I'm looking to implement a solution that takes a GPS "snapshot" when the user clicks a button. Am I right in thinking the position is only provided as part of the Location Changed event? If so, can I call this function within my click event handler to get the position, and, if so, what do I pass in for the Location argument?

I'm maybe just misunderstanding how this works.


B4X founder
Staff member
Licensed User
Longtime User
Tip: no need for the starter service with B4XPages. Better to implement the GPS functionality in one of the pages.

Private Sub GPS_LocationChanged (Location1 As Location)
   CurrentLocation = Location1 'CurrentLocation is a class global variable.
End Sub

Now you can get the current location whenever you want. Do check that CurrentLocation.IsInitialized = True.
Upvote 0


Not too familiar with B4XPages. Will need to read up on it some more. For now I've implemented your suggestion, and it's working great. Now I can call on the current location whenever I need it. Noticed the GPS initialization can take a while. Was thinking showing a message, such as "GPS Initializing..." with a Wait For to pause until that returns true. Would the service code be the best place to implement that, or the activity code?
Upvote 0

Ivica Golubovic

Active Member
Licensed User
My advice to you is to use a fused location provider instead of GPS or GNSS. It is much faster and more accurate because it uses satellites and terrestrial sources (WIFI networks, mobile networks etc.) to determine the location. It also has the GetCurrentLocation method, which refreshes the reading of the location at the moment of using the specified method and presents the current location where the device is located. The downside of the fused location provider is that it can only be used by devices that have Google Mobile Service (GMS). Visit the link below:

Thread 'FusedLocationProviderGMS (Latest)'

And follow the @Erel advice, switch to B4X pages.
Upvote 0