googlemaps / googlemaps/android-maps-compose

Setting CameraPositionState.position is not stateful

Open
#523 2 comments 0 reactions 0 assignees View on GitHub
priority: p1 type: bug
Dominant language
Kotlin
Stars
1.3k
Forks
181
Avg merge
2d 23h
Merged PRs (30d)
18

Description

android-maps-compose 4.3.3

Setting `CameraPositionState.position` is not stateful. (Setting the property calls through to the `GoogleMap` SDK and immediately changes the camera position on the map.) This is an invalid Compose architectural decision:

1. Reading this property is stateful: it reads from a state.
2. The class has *State* in its name, and `position` is its primary public property, so setting it must be stateful to not completely defy user expectations.
3. Not even the KDoc mentions setting not being stateful.
4. By contrast, setting `MarkerState.position` is stateful. The behavior of `CameraPositionState` is different when it should be the same.

The general impact is that setting it via snapshot state does not work correctly. This is a fundamental violation of normal Compose behavior.

For example, a user might attempt something like this to control camera position, which mimics the behavior of `rememberUpdatedState()`, but for `CameraPositionState` instead of `MutableState`:

```
@Composable
fun MapWithCamera(cameraPosition: CameraPosition) {
val cameraState = rememberCameraPositionState(position = cameraPosition)
.also { it.position = cameraPosition }

GoogleMap(
cameraPositionState = cameraState
)
}
```

This approach is normally sound, but not valid here, because setting `CameraStatePosition.position` is a side effect (irreversible), instead of setting snapshot state. For example, if the composition is cancelled, the new camera position remains set.

`CameraPositionState.move()` can only be called from the main thread, so disallowing setter access in favor of this method is not a great workaround.

https://github.com/googlemaps/android-maps-compose/blob/f857fc48bf1a05191d363cd8fdb19692b284fc30/maps-compose/src/main/java/com/google/maps/android/compose/CameraPositionState.kt#L97-L111

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.