GPS.Start and other questions

jpvniekerk

Active Member
Licensed User
Longtime User
I am trying to come to grips with the GPS Start function.

As I understand it, a command like
B4X:
GPS1.Start(5000,10)
will result in the GPS_LocationChanged event to be called every 5 seconds OR when the position has changed by 10 meter, whichever one occurs first.
Is this correct?
Just looking at the data captured during GPS_LocationChanged event does not appear that the above is actually happening - have not had time to analyze the data yet, but it does not seem right (i.e. if I stand still, the event is not raised every 5 seconds, and if I move fast, the event is not raised every 10 m).

The values in GPS1.Start does not seem to have any influence on the GPS_GpsStatus event. it seems to be called at "maximum frequency" regardless of the settings in GPS.Start. Is this correct?

If I set
B4X:
GPS1.Start(5000,0)
Will the GPS_LocationChanged event be called with any movement deteced?

I am trying to set up a little program to record the locations every 5 seconds, regardless of the distance difference, and another one to record the coordinates every 50 meters of distance (e.g.) regardless of the time difference. Setting the non-critical value in GPS1.Start to 0 does not seem to work for me. How should I do this?

Thanks for any clarification on this.
 

jpvniekerk

Active Member
Licensed User
Longtime User
OK...I'm impatient today :sign0095: and wrote a little routine to check the timing etc of the GPS.

Here's my findings:

GPS_GpsStatus event is raised every 1 second, regardless of the GPS.Start settings.

With
B4X:
GPS1.Start(5000,0)
GPS_LocationChanged event is raised every 5 seconds (as expected). Distance traveled does not seem to have any effect on this.

With
B4X:
GPS1.Start(0,10)
GPS_LocationChanged event is raised about every 10 meters (as expected). This was not very precise, probably because of the accuracy also being ± 10 meters - but it seems to be only reacting on the movement. Sitting still did not raise the event at all.

With
B4X:
GPS1.Start(2000,5)
Things did not work as expected. Sitting still, GPS_LocationChanged event is raised only about every 40-50 seconds - sometimes up to 200 seconds. While walking around at a steady pace, GPS_LocationChanged event is raised about every 10-15 seconds, but very inconsistent (again accuracy probably plays a part in this).

It seems then that with both values set, the time value setting is ignored. GPS1.Start(2000,5) reacts the same as GPS1.Start(0,5). This is not what the documentation leads me to understand. According to the documentation whichever value is reached first will raise the GPS_Location Changed event.

Is this a bug, or am I not interpreting the documentation correctly?

I think I also "discovered" a way to check if the GPS has lost the fix. GPS_LocationChanged event will return the last known values - even when the GPS has lost the fix.

I've added the following:
B4X:
Sub Globals
   Dim GPSFix As Boolean
End Sub

Sub GPS_GpsStatus (Satellites As List)
   GPSFix = False
   For i = 0 To Satellites.Size - 1
      Dim Satellite As GPSSatellite
      Satellite = Satellites.Get(i)
      If Satellite.UsedInFix = True Then GPSFix = True
   Next
End Sub
Whenever the GPS has a fix, GPSFix will be true, and if it loses the fix, GPSFix will be False. This can then be used as needed elsewhere in the program.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Quote from the native API documentation:
minTime the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.
minDistance the minimum distance interval for notifications, in meters
 
Upvote 0

jpvniekerk

Active Member
Licensed User
Longtime User
Erel, thanks for the reply.

I'm not worried about the absolute accuracy about the timing - within 10% will be fine for my application.

It doesn't explain why GPS1.Start(2000,50) doesn't raise the GPS_LocationChanged event every 2 seconds (or thereabouts) when I'm not moving. I've used these values in a test and have been running it now for more than 5 minutes and GPS_LocationChanged have not been raised after the first time (when the GPS got the initial fix).
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
If you check the location manager documentation on the android dev site, if you specify both time and distance then both of them have to be true for the event to be raised. Have a look at my extended location manager library you could use the proximity alert with center set at current position then react to the exit event, stop proximity alert, set a new alert with new position then loop again. I have not tried this but should work.

Sent from my HTC One X using Tapatalk 2
 
Upvote 0

jpvniekerk

Active Member
Licensed User
Longtime User
Thanks lagore.

This is in line with what I am seeing as well, but the complete opposite of Erel's remark in the GPS Tutorial Post
The Start method receives two values which are the minimum time (milliseconds) and the minimum distance (meters) between events. An event will be raised when at least one of these criterions is met.

Erel, please verify this.
 
Upvote 0

jpvniekerk

Active Member
Licensed User
Longtime User
I found another possible problem:

The program in Erel's Tutorial is as follows:

B4X:
Sub Activity_Resume
    If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        GPS1.Start(0, 0) 'Listen to GPS with no filters.
    End If
End Sub

This works ONLY if you use GPS1.Start(0,0). If you want to use anything else here, the GPS will start and skip the instruction you give to start the GPS in the program. By default, then, the GPS will run with a (0,0) start.
If the GPS was already turned on in the device when the program starts, it will pick up your instruction.

To enable the GPS1.Start variables, as intended, the GPS1.Start command must be moved outside the If-End If block.

To make it work (consistently) with other values, I had to change the program as follows:
B4X:
Sub Activity_Resume
    If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
        GPS1.Stop
    End If
    GPS1.Start(5000, 0) 'Listen to GPS with filters.
End Sub

If you do not have the GPS1.Stop before you exit the If-End If block, the GPS1.Start(5000,0) is ignored!
 
Last edited:
Upvote 0

lagore

Active Member
Licensed User
Longtime User
I checked the GPS library and it uses the standard LocationManager, a quote from that methrod
The minDistance parameter can also be used to control the frequency of location updates. If it is greater than 0 then the location provider will only send your application an update when the location has changed by at least minDistance meters, AND at least minTime milliseconds have passed.
therefore both time and distance have to change with this method.
 
Upvote 0
Top