Try to get location by using all available providers.

This commit is contained in:
Onuray Sahin 2022-01-28 14:17:07 +03:00
parent 26c0fee56d
commit 0f8c3bcc17
1 changed files with 20 additions and 21 deletions

View File

@ -55,29 +55,28 @@ class LocationTracker @Inject constructor(
Timber.d("## LocationTracker. isGpsEnabled: $isGpsEnabled - isNetworkEnabled: $isNetworkEnabled")
val provider = when {
isGpsEnabled -> LocationManager.GPS_PROVIDER
isNetworkEnabled -> LocationManager.NETWORK_PROVIDER
else -> {
callback?.onLocationProviderIsNotAvailable()
Timber.v("## LocationTracker. There is no location provider available")
return
}
}
locationManager.allProviders
.takeIf { it.isNotEmpty() }
?.forEach { provider ->
Timber.d("## LocationTracker. track location using $provider")
// Send last known location without waiting location updates
locationManager.getLastKnownLocation(provider)?.let { lastKnownLocation ->
Timber.d("## LocationTracker. lastKnownLocation")
callback?.onLocationUpdate(lastKnownLocation.toLocationData())
}
// Send last known location without waiting location updates
locationManager.getLastKnownLocation(provider)?.let { lastKnownLocation ->
Timber.d("## LocationTracker. lastKnownLocation")
callback?.onLocationUpdate(lastKnownLocation.toLocationData())
}
Timber.d("## LocationTracker. track location using $provider")
locationManager.requestLocationUpdates(
provider,
MIN_TIME_TO_UPDATE_LOCATION_MILLIS,
MIN_DISTANCE_TO_UPDATE_LOCATION_METERS,
this
)
locationManager.requestLocationUpdates(
provider,
MIN_TIME_TO_UPDATE_LOCATION_MILLIS,
MIN_DISTANCE_TO_UPDATE_LOCATION_METERS,
this
)
}
?: run {
callback?.onLocationProviderIsNotAvailable()
Timber.v("## LocationTracker. There is no location provider available")
}
}
@RequiresPermission(anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION])