Skip to content

Commit 5de1d60

Browse files
authored
docs: improved documentation for camera state (#746)
1 parent 3ac3f52 commit 5de1d60

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,26 @@ Box(Modifier.fillMaxSize()) {
157157
}
158158
```
159159

160+
Remember that the map must load before any camera state can be set. If you are using a LaunchedEffect, you must wait until the map has been loaded:
161+
162+
```kotlin
163+
@Composable
164+
fun MapScreen() {
165+
var mapLoaded by remember { mutableStateOf(false) }
166+
167+
GoogleMap(
168+
modifier = Modifier.fillMaxSize(),
169+
onMapLoaded = { mapLoaded = true }
170+
)
171+
172+
if (mapLoaded) {
173+
LaunchedEffect(Unit) {
174+
// here the camera operations
175+
}
176+
}
177+
}
178+
```
179+
160180
</details>
161181

162182
<details>

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,16 @@ class LocationTrackingActivity : ComponentActivity() {
9393
// Collect location updates
9494
val locationState = locationFlow.collectAsState(initial = newLocation())
9595

96-
// Update blue dot and camera when the location changes
97-
LaunchedEffect(locationState.value) {
98-
Log.d(TAG, "Updating blue dot on map...")
99-
locationSource.onLocationChanged(locationState.value)
100-
101-
Log.d(TAG, "Updating camera position...")
102-
val cameraPosition = CameraPosition.fromLatLngZoom(LatLng(locationState.value.latitude, locationState.value.longitude), zoom)
103-
cameraPositionState.animate(CameraUpdateFactory.newCameraPosition(cameraPosition), 1_000)
96+
if (isMapLoaded) {
97+
// Update blue dot and camera when the location changes
98+
LaunchedEffect(locationState.value) {
99+
Log.d(TAG, "Updating blue dot on map...")
100+
locationSource.onLocationChanged(locationState.value)
101+
102+
Log.d(TAG, "Updating camera position...")
103+
val cameraPosition = CameraPosition.fromLatLngZoom(LatLng(locationState.value.latitude, locationState.value.longitude), zoom)
104+
cameraPositionState.animate(CameraUpdateFactory.newCameraPosition(cameraPosition), 1_000)
105+
}
104106
}
105107

106108
// Detect when the map starts moving and print the reason

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ import kotlin.coroutines.resumeWithException
4343
/**
4444
* Create and [rememberSaveable] a [CameraPositionState] using [CameraPositionState.Saver].
4545
* [init] will be called when the [CameraPositionState] is first created to configure its
46-
* initial state.
46+
* initial state. Remember that the camera state can be applied when the map has been
47+
* loaded.
4748
*/
4849
@Composable
4950
public inline fun rememberCameraPositionState(

0 commit comments

Comments
 (0)