Reputation: 1
I am slightly new to kotlin, so this may be a super easy fix. Basically, I followed a tutorial to get gps locations using the fusedLocationProviderClient and I am trying to stop the looper once it has found a location (i.e. I want it to find a location and then stop). I'm not very familiar with loopers, but how do I create a looper that works with the fusedLocationProviderClient that I can quit after I find a location.
Here is the code:
private fun setUpLocationListener() {
checkSmsPermission(PERMISSION_FINE_LOCATION)
val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
// for getting the current location update after every 2 seconds with high accuracy
val locationRequest = LocationRequest().setInterval(2000).setFastestInterval(2000)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
fusedLocationProviderClient.requestLocationUpdates(
locationRequest,
object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
Log.d(TAG, "Location found")
super.onLocationResult(locationResult)
for (location in locationResult.locations) {
Log.d(TAG, location.latitude.toString())
Log.d(TAG,location.longitude.toString())
}
if (locationResult.locations.size > 0){
// I want to quit here
}
}
},
Looper.myLooper()!!
)
}
Mostly I think I need a bit of help on how to use loopers with fusedLocationProviderClient to create a looper that I can quit from inside.
Thanks in advance for your help
Upvotes: 0
Views: 66
Reputation: 1
For those who may have had the same questions as me, here's what I did: for the location request variable, I just removed the ".setInterval(2000).setFastestInterval(2000)". Obviously, with these enabled, the GPS location always was going to get refound.
Upvotes: 0
Reputation: 12953
You could removeLocationUpdates
from fusedClient
to stop receiving updates
override fun onLocationResult(locationResult: LocationResult) {
Log.d(TAG, "Location found")
super.onLocationResult(locationResult)
for (location in locationResult.locations) {
Log.d(TAG, location.latitude.toString())
Log.d(TAG,location.longitude.toString())
}
if (locationResult.locations.size > 0){
fusedLocationProviderClient.removeLocationUpdates(this)
}
}
Upvotes: 1