Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@ Box(Modifier.fillMaxSize()) {
}
```

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:

```kotlin
@Composable
fun MapScreen() {
var mapLoaded by remember { mutableStateOf(false) }

GoogleMap(
modifier = Modifier.fillMaxSize(),
onMapLoaded = { mapLoaded = true }
)

if (mapLoaded) {
LaunchedEffect(Unit) {
// here the camera operations
}
}
}
```

</details>

<details>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,16 @@ class LocationTrackingActivity : ComponentActivity() {
// Collect location updates
val locationState = locationFlow.collectAsState(initial = newLocation())

// Update blue dot and camera when the location changes
LaunchedEffect(locationState.value) {
Log.d(TAG, "Updating blue dot on map...")
locationSource.onLocationChanged(locationState.value)

Log.d(TAG, "Updating camera position...")
val cameraPosition = CameraPosition.fromLatLngZoom(LatLng(locationState.value.latitude, locationState.value.longitude), zoom)
cameraPositionState.animate(CameraUpdateFactory.newCameraPosition(cameraPosition), 1_000)
if (isMapLoaded) {
// Update blue dot and camera when the location changes
LaunchedEffect(locationState.value) {
Log.d(TAG, "Updating blue dot on map...")
locationSource.onLocationChanged(locationState.value)

Log.d(TAG, "Updating camera position...")
val cameraPosition = CameraPosition.fromLatLngZoom(LatLng(locationState.value.latitude, locationState.value.longitude), zoom)
cameraPositionState.animate(CameraUpdateFactory.newCameraPosition(cameraPosition), 1_000)
}
}

// Detect when the map starts moving and print the reason
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ import kotlin.coroutines.resumeWithException
/**
* Create and [rememberSaveable] a [CameraPositionState] using [CameraPositionState.Saver].
* [init] will be called when the [CameraPositionState] is first created to configure its
* initial state.
* initial state. Remember that the camera state can be applied when the map has been
* loaded.
*/
@Composable
public inline fun rememberCameraPositionState(
Expand Down
Loading