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
You could
removeLocationUpdatesfromfusedClientto stop receiving updates