Could you please help me? I have a task to make a GPS tracker that would pass GPS data to Firebase. But the key point is that the tracker should have the following sensitivity - 60 meters (10 for test build), period every 10 minutes (5 seconds for test build).
As I understood it should track each 10 min only if you pass 60 meters. So if I passed 40 meters it shouldn't track it.
In the Usecase I wrote the following method with interval, smallestDisplacement but it doesn't work as I expected. Could you please help me with it?
@SuppressLint("MissingPermission")
fun saveLocation() {
val locationRequest = LocationRequest().apply {
interval = com.example.gpstracker.BuildConfig.TRACKING_INTERVAL_MILLIS
fastestInterval = com.example.gpstracker.BuildConfig.TRACKING_INTERVAL_MILLIS
smallestDisplacement = com.example.gpstracker.BuildConfig.TRACKING_INTERVAL_METERS.toFloat()
}
val locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
locationResult.locations.let { locations ->
val location = locations.last()
saveLocationToFirebase(location.latitude, location.longitude)
}
}
}
fusedLocationClient.requestLocationUpdates(
locationRequest,
locationCallback,
Looper.myLooper()
)
}
Both of the parameters you are setting are minimums, so you will get a locationCallBack when either the minimum time or the minimum distance are passed.
I suggest that you just set the minimum time and every 10 minutes you will get a locationCallBack, where you can deal with the distance threshold yourself.
calculate the distance between this location and last location ( In Kotlin I would use distance = lastLocation.distanceTo(location) ) if it is greater than your threshold then process it set last location = this location for next time (In Kotlin this would be lastLocation.set(location) ) else do nothing