Skip to content

Commit e42f3b9

Browse files
authored
feat: deprecate rememberCameraPositionState (#749)
1 parent 7fb1c9b commit e42f3b9

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

maps-app/src/main/java/com/google/maps/android/compose/MapsInLazyColumnActivity.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ fun MapsInLazyColumn(
152152

153153
val cameraPositionStates = mapItems.associate { item ->
154154
item.id to rememberCameraPositionState(
155-
key = item.id,
156155
init = { position = CameraPosition.fromLatLngZoom(item.location, item.zoom) }
157156
)
158157
}

maps-compose/src/main/java/com/google/maps/android/compose/CameraPositionState.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,35 @@ import java.lang.Integer.MAX_VALUE
4040
import kotlin.coroutines.resume
4141
import kotlin.coroutines.resumeWithException
4242

43+
/**
44+
* Creates and remembers a [CameraPositionState] using [rememberSaveable].
45+
*
46+
* The camera position state is saved across configuration changes and process death,
47+
* ensuring the map retains its last position.
48+
*
49+
* @param init A lambda that is called when the [CameraPositionState] is first created to
50+
* configure its initial state, such as its position or zoom level.
51+
*/
52+
@Composable
53+
public inline fun rememberCameraPositionState(
54+
crossinline init: CameraPositionState.() -> Unit = {}
55+
): CameraPositionState = rememberSaveable(saver = CameraPositionState.Saver) {
56+
CameraPositionState().apply(init)
57+
}
58+
4359
/**
4460
* Create and [rememberSaveable] a [CameraPositionState] using [CameraPositionState.Saver].
4561
* [init] will be called when the [CameraPositionState] is first created to configure its
4662
* initial state. Remember that the camera state can be applied when the map has been
4763
* loaded.
4864
*/
65+
@Deprecated(
66+
message = "The 'key' parameter is deprecated. Please use the new `rememberCameraPositionState` function without a key.",
67+
replaceWith = ReplaceWith(
68+
"rememberCameraPositionState(init)",
69+
"com.google.maps.android.compose.rememberCameraPositionState"
70+
)
71+
)
4972
@Composable
5073
public inline fun rememberCameraPositionState(
5174
key: String? = null,

0 commit comments

Comments
 (0)