From 0caa4980b02856b14e78731b5dda0a3e1ed81e41 Mon Sep 17 00:00:00 2001 From: CaolanCode Date: Wed, 3 Apr 2024 12:42:16 +0100 Subject: [PATCH 1/2] For the Recomposition Demo, add the fourth Signapose location and centre the camera view when the location changes --- .../android/compose/RecompositionActivity.kt | 18 ++++++++++-------- .../android/compose/CameraPositionState.kt | 1 - 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/google/maps/android/compose/RecompositionActivity.kt b/app/src/main/java/com/google/maps/android/compose/RecompositionActivity.kt index 1154dc51..ab17b906 100644 --- a/app/src/main/java/com/google/maps/android/compose/RecompositionActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/RecompositionActivity.kt @@ -27,7 +27,9 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier +import com.google.android.gms.maps.model.CameraPosition import com.google.android.gms.maps.model.Marker import com.google.maps.android.compose.theme.MapsComposeSampleTheme import kotlin.random.Random @@ -44,14 +46,10 @@ class RecompositionActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { - val cameraPositionState = rememberCameraPositionState { - position = defaultCameraPosition - } Box(Modifier.fillMaxSize()) { MapsComposeSampleTheme { GoogleMapView( - modifier = Modifier.matchParentSize(), - cameraPositionState = cameraPositionState + modifier = Modifier.matchParentSize() ) } } @@ -61,10 +59,13 @@ class RecompositionActivity : ComponentActivity() { @Composable fun GoogleMapView( modifier: Modifier = Modifier, - cameraPositionState: CameraPositionState = rememberCameraPositionState(), content: @Composable () -> Unit = {}, ) { val markerState = rememberMarkerState(position = singapore) + val cameraPosition = CameraPosition.fromLatLngZoom(markerState.position, 11f) + val cameraPositionState = rememberCameraPositionState { + position = cameraPosition + } val uiSettings by remember { mutableStateOf(MapUiSettings(compassEnabled = false)) } val mapProperties by remember { @@ -100,13 +101,14 @@ class RecompositionActivity : ComponentActivity() { } Column { Button(onClick = { - val randomValue = Random.nextInt(3) + val randomValue = Random.nextInt(4) markerState.position = when (randomValue) { 0 -> singapore 1 -> singapore2 2 -> singapore3 - else -> singapore + else -> singapore4 } + cameraPositionState.position = CameraPosition.fromLatLngZoom(markerState.position, 11f) }) { Text("Change Location") } diff --git a/maps-compose/src/main/java/com/google/maps/android/compose/CameraPositionState.kt b/maps-compose/src/main/java/com/google/maps/android/compose/CameraPositionState.kt index 776833d2..ab822265 100644 --- a/maps-compose/src/main/java/com/google/maps/android/compose/CameraPositionState.kt +++ b/maps-compose/src/main/java/com/google/maps/android/compose/CameraPositionState.kt @@ -32,7 +32,6 @@ import com.google.android.gms.maps.model.LatLng import kotlinx.coroutines.CancellableContinuation import kotlinx.coroutines.CancellationException import kotlinx.coroutines.Job -import kotlinx.coroutines.cancel import kotlinx.coroutines.currentCoroutineContext import kotlinx.coroutines.suspendCancellableCoroutine import java.lang.Integer.MAX_VALUE From 01b4605b91ded5779f883ee4e1ec0aa19a84829d Mon Sep 17 00:00:00 2001 From: CaolanCode Date: Wed, 3 Apr 2024 12:42:16 +0100 Subject: [PATCH 2/2] chore: Recomposition demo, updates Camera --- .../android/compose/RecompositionActivity.kt | 18 ++++++++++-------- .../android/compose/CameraPositionState.kt | 1 - 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/google/maps/android/compose/RecompositionActivity.kt b/app/src/main/java/com/google/maps/android/compose/RecompositionActivity.kt index 1154dc51..ab17b906 100644 --- a/app/src/main/java/com/google/maps/android/compose/RecompositionActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/RecompositionActivity.kt @@ -27,7 +27,9 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier +import com.google.android.gms.maps.model.CameraPosition import com.google.android.gms.maps.model.Marker import com.google.maps.android.compose.theme.MapsComposeSampleTheme import kotlin.random.Random @@ -44,14 +46,10 @@ class RecompositionActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { - val cameraPositionState = rememberCameraPositionState { - position = defaultCameraPosition - } Box(Modifier.fillMaxSize()) { MapsComposeSampleTheme { GoogleMapView( - modifier = Modifier.matchParentSize(), - cameraPositionState = cameraPositionState + modifier = Modifier.matchParentSize() ) } } @@ -61,10 +59,13 @@ class RecompositionActivity : ComponentActivity() { @Composable fun GoogleMapView( modifier: Modifier = Modifier, - cameraPositionState: CameraPositionState = rememberCameraPositionState(), content: @Composable () -> Unit = {}, ) { val markerState = rememberMarkerState(position = singapore) + val cameraPosition = CameraPosition.fromLatLngZoom(markerState.position, 11f) + val cameraPositionState = rememberCameraPositionState { + position = cameraPosition + } val uiSettings by remember { mutableStateOf(MapUiSettings(compassEnabled = false)) } val mapProperties by remember { @@ -100,13 +101,14 @@ class RecompositionActivity : ComponentActivity() { } Column { Button(onClick = { - val randomValue = Random.nextInt(3) + val randomValue = Random.nextInt(4) markerState.position = when (randomValue) { 0 -> singapore 1 -> singapore2 2 -> singapore3 - else -> singapore + else -> singapore4 } + cameraPositionState.position = CameraPosition.fromLatLngZoom(markerState.position, 11f) }) { Text("Change Location") } diff --git a/maps-compose/src/main/java/com/google/maps/android/compose/CameraPositionState.kt b/maps-compose/src/main/java/com/google/maps/android/compose/CameraPositionState.kt index 776833d2..ab822265 100644 --- a/maps-compose/src/main/java/com/google/maps/android/compose/CameraPositionState.kt +++ b/maps-compose/src/main/java/com/google/maps/android/compose/CameraPositionState.kt @@ -32,7 +32,6 @@ import com.google.android.gms.maps.model.LatLng import kotlinx.coroutines.CancellableContinuation import kotlinx.coroutines.CancellationException import kotlinx.coroutines.Job -import kotlinx.coroutines.cancel import kotlinx.coroutines.currentCoroutineContext import kotlinx.coroutines.suspendCancellableCoroutine import java.lang.Integer.MAX_VALUE