Pablo
02/27/2025, 2:45 PMPablo
02/27/2025, 2:45 PMfun transformStringLocationIntoLatLng(context: Context, address: String, addressType: AddressType) {
viewModelScope.launch(Dispatchers.IO) {
_uiState.update { currentState -> currentState.copy(loading = true) }
val geocoder = Geocoder(context)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
geocoder.getFromLocationName(address, 1, object : Geocoder.GeocodeListener {
override fun onGeocode(addresses: List<Address>) {
if (addresses.isNotEmpty()) {
val location = addresses[0]
val latLng = LatLng(location.latitude, location.longitude)
when (addressType) {
AddressType.ORIGIN ->
_uiState.update { currentState -> currentState.copy(loading = false, originLocation = latLng)}
AddressType.DESTINATION ->
_uiState.update { currentState -> currentState.copy(loading = false, destinationLocation = latLng)}
}
}
}
override fun onError(errorMessage: String?) {
Log.d("XXXX", "GeocodeListener ERROR: $errorMessage")
}
})
} else {
val addresses = geocoder.getFromLocationName(address, 1)
if (addresses?.isNotEmpty() == true) {
val location = addresses[0]
val latLng = LatLng(location.latitude, location.longitude)
when (addressType) {
AddressType.ORIGIN ->
_uiState.update { currentState -> currentState.copy(loading = false, originLocation = latLng)}
AddressType.DESTINATION ->
_uiState.update { currentState -> currentState.copy(loading = false, destinationLocation = latLng)}
}
}
}
}
}
François
02/27/2025, 2:54 PMPablo
02/27/2025, 2:57 PMPablo
02/27/2025, 2:57 PMsuspendCancellableCoroutine { continuation ->
val geocoder = Geocoder(context, Locale.getDefault())
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
geocoder.getFromLocationName(address, 1, object : Geocoder.GeocodeListener {
override fun onGeocode(addresses: List<Address>) {
if (addresses.isNotEmpty()) {
val location = addresses[0]
continuation.resume(LatLng(location.latitude, location.longitude))
} else {
continuation.resume(null)
}
}
override fun onError(errorMessage: String?) {
continuation.resumeWithException(Exception(errorMessage))
}
})
Pablo
02/27/2025, 2:57 PMFrançois
02/27/2025, 2:58 PMPablo
02/27/2025, 2:58 PMFrançois
02/27/2025, 3:00 PMPablichjenkov
02/27/2025, 3:01 PMFlow.callbackFlow
Pablo
02/27/2025, 3:02 PMPablo
02/27/2025, 3:02 PMPablo
02/27/2025, 3:02 PMPablo
02/27/2025, 3:02 PMPablichjenkov
02/27/2025, 3:22 PMPablichjenkov
02/27/2025, 3:22 PMPablo
02/27/2025, 4:44 PMPablo
02/27/2025, 4:44 PMPablichjenkov
02/27/2025, 5:13 PMPablo
02/27/2025, 5:16 PMPablo
02/27/2025, 5:16 PMPablo
02/27/2025, 5:17 PMPablo
02/27/2025, 5:52 PMPablo
02/27/2025, 5:52 PMPablichjenkov
02/27/2025, 5:57 PMPablichjenkov
02/27/2025, 5:58 PMPablo
02/27/2025, 6:14 PMPablo
02/27/2025, 6:14 PM