I'm trying to get the user's speed 10 times in three seconds when a function is called, and then calculate the sum of all the gotten values. I have the global variable:
lateinit var fusedLocationProviderClient : FusedLocationProviderClient
Which I then initialize in the onCreate
method like this:
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
And then this is the code where I try to calculate the sum of the speeds:
val task = fusedLocationProviderClient.lastLocation
var sumSpeed = 0F
task.addOnSuccessListener {
if (it != null) {
for (i in 1..10) {
Thread.sleep(300)
sumSpeed += it.speed
}
}
current_gps_speed_tv.text = "Total is: $sumSpeed"
if (sumSpeed < 25) {
...
}
}
The result of sumSpeed
always remains the same (34m/s), which is 10 times it.speed
, which is also the same every time (3.4m/s).
I've searched for other answers (like this one) but what I'm using seems to be different. Why does this happen? Thank you.