Android Question GPS Problem

bashka_abdyli

Member
Licensed User
Longtime User
Hi There,

We have build an app that sends GPS location of android device into a DB. All works fine. But sometimes the location is wrong, It jumps at other country. In the device the GPS is enabled, and getting location based on wireless of celular data is disabled.

Is there any way to prevent these jumps ? Below is the code that we use.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim gpsClient As GPS
      Dim userLocation As Location
    Dim Completed As Boolean
    '   
End Sub
Sub Service_Start (StartingIntent As Intent)

    gpsClient.Initialize("gpsClient")
    userLocation.Initialize

    If gpsClient.GPSEnabled=False Then
      ToastMessageShow("Please enable your device's GPS capabilities", True)
        StartActivity(gpsClient.LocationSettingsIntent)
  Else
      gpsClient.Start(0, 0)
    '  ProgressDialogShow("Waiting for GPS location")
  End If

End Sub

Sub ExecuteRemoteQuery(Query As String, JobName As String)

    Dim job As HttpJob
    job.Initialize(JobName, Me)
    job.PostString("blah blah.php", Query)

End Sub

Sub JobDone1(Job As HttpJob)
    ProgressDialogHide
End Sub

Sub gpsClient_LocationChanged (gpsLocation As Location)
 
    ' ProgressDialogHide
    userLocation=gpsLocation
    gpsClient.Stop
  ' StartActivity("actDisplayMap")
    Dim mystring As String
   
    Dim p As Phone
    mystring = p.GetSettings("android_id")
    Dim devmodel As String
    devmodel = p.Model
   
    Dim speed As Float
    speed = userLocation.speed

    Dim bearing As Float
    bearing = userLocation.bearing

    ExecuteRemoteQuery("insert into GPSLog (latitude,longitude,deviceid,speed,bearing) values(" & userLocation.Latitude & "," & userLocation.Longitude & ",'" & mystring & "'," & speed & "," & bearing & ")","job")
     
    StartServiceAt("", DateTime.Now + 15 * DateTime.TicksPerSecond , True)
   
End Sub

Sub Service_Destroy

End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use Location.DistanceTo method to calculate the distance. You will need to decide what is normal and what is not.

I would first change the way the program works:
1. Move the GPS.Initialize and start code to Service_Create.
2. Use a regular timer to take the location every 15 seconds (with the timer you can ignore multiple events that happen in the same interval).
3. Call StartServiceAt in Service_Start to schedule the service to start in 5 minutes. This will only make sure that the process will be started again if it was killed by the OS.

Other options: Creating a sticky service - long running background tasks
 
Upvote 0

bashka_abdyli

Member
Licensed User
Longtime User
Can you show me, how to do this. I would really appreciate it.

I would first change the way the program works:
1. Move the GPS.Initialize and start code to Service_Create.
2. Use a regular timer to take the location every 15 seconds (with the timer you can ignore multiple events that happen in the same interval).
3. Call StartServiceAt in Service_Start to schedule the service to start in 5 minutes. This will only make sure that the process will be started again if it was killed by the OS.
 
Upvote 0

bashka_abdyli

Member
Licensed User
Longtime User
The distance between the points is now settled. Thanks.

But you gave me also these suggestions:
"
I would first change the way the program works:
1. Move the GPS.Initialize and start code to Service_Create.
2. Use a regular timer to take the location every 15 seconds (with the timer you can ignore multiple events that happen in the same interval).
3. Call StartServiceAt in Service_Start to schedule the service to start in 5 minutes. This will only make sure that the process will be started again if it was killed by the OS."

Please show me how to do that if possible. If you can modify me code above.
 
Upvote 0
Top