Billy Newman
06/28/2022, 1:45 PMBilly Newman
06/28/2022, 1:49 PMdata class Location(val latitude: Double, val longitude: Double)
@OptIn(MapsComposeExperimentalApi::class)
@Composable
private fun MapScreen(mapDestination : Location? = null) {
val mapOrigin by viewModel.mapOrigin.observeAsState()
// Pass both the origin and destination to the maps composable
Map(mapOrigin, mapDestination)
}
@OptIn(MapsComposeExperimentalApi::class)
@Composable
private fun Map(
origin: Location?,
destination: Location?
) {
val cameraPositionState: CameraPositionState = rememberCameraPositionState {}
LaunchedEffect(origin) {
origin?.let { origin ->
cameraPositionState.position = CameraPosition.fromLatLngZoom(LatLng(origin.latitude, origin.longitude), origin.zoom.toFloat())
}
}
LaunchedEffect(destination) {
destination?.let { destination ->
scope.launch {
val update = CameraUpdateFactory.newLatLngZoom(LatLng(destination.latitude, destination.longitude), destination.zoom.toFloat())
cameraPositionState.animate(update)
}
}
}
}
When launching the MapScreen from another composable with a new destination starting at the origin and animating to the destination works ~75% of the time. Possibly the map origin from the data store is coming in after the destination from the DataStore’s flow.
Am I even in on the right track with thinking in compose on this one?Csaba Szugyiczki
06/28/2022, 1:52 PMMapScreen
so you can make sure if the bug is caused by it, or not.Csaba Szugyiczki
06/28/2022, 1:54 PMCsaba Szugyiczki
06/28/2022, 1:56 PMif (origin != null && destination != null) {
LaunchedEffect(Unit) {
cameraPositionState.position = CameraPosition.fromLatLngZoom(LatLng(origin.latitude, origin.longitude), origin.zoom.toFloat())
val update = CameraUpdateFactory.newLatLngZoom(LatLng(destination.latitude, destination.longitude), destination.zoom.toFloat())
cameraPositionState.animate(update)
}
}
Billy Newman
06/28/2022, 2:06 PMBilly Newman
06/28/2022, 2:13 PMCsaba Szugyiczki
06/28/2022, 2:16 PMBilly Newman
06/28/2022, 2:23 PMCsaba Szugyiczki
06/28/2022, 2:24 PMColton Idle
06/29/2022, 2:13 AMBilly Newman
06/29/2022, 2:35 AMColton Idle
06/29/2022, 2:08 PMChris Arriola
06/29/2022, 5:49 PM