Skip to content

Instantly share code, notes, and snippets.

@yusufceylan
Created September 7, 2020 10:39
Show Gist options
  • Save yusufceylan/aae842836e175616b7aec467931d0699 to your computer and use it in GitHub Desktop.
Save yusufceylan/aae842836e175616b7aec467931d0699 to your computer and use it in GitHub Desktop.
Huawei Location Kit - Last Known Location
fun getLastKnownLocation(
onSuccess: ((lastKnownLocation: Location?) -> Unit)? = null,
onFail: ((exception: Exception) -> Unit)? = null
) {
val task: Task<Location> = mFusedLocationProviderClient.lastLocation
task.addOnSuccessListener { lastKnowLocation ->
if (lastKnowLocation == null) {
LogUtils.d("LocationKit -> Last Known Location is empty")
} else {
val currentLatLng = LatLng(
lastKnowLocation.latitude,
lastKnowLocation.longitude
)
LogUtils.d("LocationKit -> Last Known Location: $currentLatLng")
}
// Notify
onSuccess?.invoke(lastKnowLocation)
}.addOnFailureListener { exception ->
LogUtils.d("LocationKit -> Failed to get Last Known Location with exception: ${exception.localizedMessage}")
// Notify
onFail?.invoke(exception)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment