diff --git a/lib-compose/src/main/java/com/what3words/components/compose/maps/W3WMapComponent.kt b/lib-compose/src/main/java/com/what3words/components/compose/maps/W3WMapComponent.kt index 6f141994..6cf036e3 100644 --- a/lib-compose/src/main/java/com/what3words/components/compose/maps/W3WMapComponent.kt +++ b/lib-compose/src/main/java/com/what3words/components/compose/maps/W3WMapComponent.kt @@ -20,6 +20,9 @@ import com.what3words.components.compose.maps.state.W3WMapState import com.what3words.components.compose.maps.state.camera.W3WCameraState import com.what3words.core.types.common.W3WError import com.what3words.core.types.geometry.W3WCoordinates +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.launch /** * A composable function that displays a What3Words (W3W) map. @@ -311,20 +314,21 @@ private fun fetchCurrentLocation( mapManager: W3WMapManager, onError: ((W3WError) -> Unit)? = null ) { - // Fetch location - locationSource?.fetchLocation( - onLocationFetched = { location -> - // Update camera state - mapManager.moveToPosition( - coordinates = W3WCoordinates(location.latitude, location.longitude), - animate = true - ) - //TODO: Update button state - }, - onError = { error -> - onError?.invoke(W3WError("Location fetch failed: ${error.message}")) + locationSource?.let { + CoroutineScope(IO).launch { + try { + val location = it.fetchLocation() + // Update camera state + mapManager.moveToPosition( + coordinates = W3WCoordinates(location.latitude, location.longitude), + animate = true + ) + //TODO: Update button state + } catch (e: Exception) { + onError?.invoke(W3WError("Location fetch failed: ${e.message}")) + } } - ) + } } diff --git a/lib-compose/src/main/java/com/what3words/components/compose/maps/models/W3WLocationSource.kt b/lib-compose/src/main/java/com/what3words/components/compose/maps/models/W3WLocationSource.kt index 66167373..47b8bab1 100644 --- a/lib-compose/src/main/java/com/what3words/components/compose/maps/models/W3WLocationSource.kt +++ b/lib-compose/src/main/java/com/what3words/components/compose/maps/models/W3WLocationSource.kt @@ -1,10 +1,12 @@ package com.what3words.components.compose.maps.models import android.location.Location +import kotlinx.coroutines.flow.StateFlow interface W3WLocationSource { - fun fetchLocation( - onLocationFetched: (Location) -> Unit, - onError: (Exception) -> Unit, - ) + // hasPermission && isLocationEnabled + val isActive: StateFlow + + // Trigger fetch current location + suspend fun fetchLocation(): Location } \ No newline at end of file