diff --git a/app/src/main/java/com/google/maps/android/compose/BasicMapActivity.kt b/app/src/main/java/com/google/maps/android/compose/BasicMapActivity.kt index 0fd27819c..5aae2cf9a 100644 --- a/app/src/main/java/com/google/maps/android/compose/BasicMapActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/BasicMapActivity.kt @@ -1,4 +1,4 @@ -// Copyright 2021 Google LLC +// Copyright 2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -53,6 +53,8 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.testTag +import androidx.compose.ui.semantics.clearAndSetSemantics +import androidx.compose.ui.semantics.semantics import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp @@ -85,7 +87,6 @@ class BasicMapActivity : ComponentActivity() { Box(Modifier.fillMaxSize()) { GoogleMapView( - modifier = Modifier.matchParentSize(), cameraPositionState = cameraPositionState, onMapLoaded = { isMapLoaded = true @@ -113,7 +114,6 @@ class BasicMapActivity : ComponentActivity() { @Composable fun GoogleMapView( - modifier: Modifier = Modifier, cameraPositionState: CameraPositionState = rememberCameraPositionState(), onMapLoaded: () -> Unit = {}, content: @Composable () -> Unit = {} @@ -137,7 +137,9 @@ fun GoogleMapView( if (mapVisible) { GoogleMap( - modifier = modifier, + modifier = Modifier + .semantics(mergeDescendants = true) { } + .clearAndSetSemantics { }, cameraPositionState = cameraPositionState, properties = mapProperties, uiSettings = uiSettings, @@ -223,7 +225,8 @@ fun GoogleMapView( MapButton( text = "Toggle Map", onClick = { mapVisible = !mapVisible }, - modifier = Modifier.testTag("toggleMapVisibility"), + modifier = Modifier + .testTag("toggleMapVisibility") ) } val coroutineScope = rememberCoroutineScope() @@ -349,6 +352,6 @@ private fun DebugView( @Composable fun GoogleMapViewPreview() { MapsComposeSampleTheme { - GoogleMapView(Modifier.fillMaxSize()) + GoogleMapView() } } diff --git a/maps-compose/src/main/java/com/google/maps/android/compose/GoogleMap.kt b/maps-compose/src/main/java/com/google/maps/android/compose/GoogleMap.kt index dff08bb0e..58e3f27af 100644 --- a/maps-compose/src/main/java/com/google/maps/android/compose/GoogleMap.kt +++ b/maps-compose/src/main/java/com/google/maps/android/compose/GoogleMap.kt @@ -1,4 +1,4 @@ -// Copyright 2021 Google LLC +// Copyright 2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -94,53 +94,57 @@ public fun GoogleMap( Box(modifier = modifier) return } + Box( + modifier = modifier + ) { + val context = LocalContext.current + val mapView = remember { MapView(context, googleMapOptionsFactory()) } - val context = LocalContext.current - val mapView = remember { MapView(context, googleMapOptionsFactory()) } - - AndroidView(modifier = modifier, factory = { mapView }) - MapLifecycle(mapView) + AndroidView(modifier = modifier, factory = { mapView }) + MapLifecycle(mapView) - // rememberUpdatedState and friends are used here to make these values observable to - // the subcomposition without providing a new content function each recomposition - val mapClickListeners = remember { MapClickListeners() }.also { - it.indoorStateChangeListener = indoorStateChangeListener - it.onMapClick = onMapClick - it.onMapLongClick = onMapLongClick - it.onMapLoaded = onMapLoaded - it.onMyLocationButtonClick = onMyLocationButtonClick - it.onMyLocationClick = onMyLocationClick - it.onPOIClick = onPOIClick - } - val currentLocationSource by rememberUpdatedState(locationSource) - val currentCameraPositionState by rememberUpdatedState(cameraPositionState) - val currentContentPadding by rememberUpdatedState(contentPadding) - val currentUiSettings by rememberUpdatedState(uiSettings) - val currentMapProperties by rememberUpdatedState(properties) + // rememberUpdatedState and friends are used here to make these values observable to + // the subcomposition without providing a new content function each recomposition + val mapClickListeners = remember { MapClickListeners() }.also { + it.indoorStateChangeListener = indoorStateChangeListener + it.onMapClick = onMapClick + it.onMapLongClick = onMapLongClick + it.onMapLoaded = onMapLoaded + it.onMyLocationButtonClick = onMyLocationButtonClick + it.onMyLocationClick = onMyLocationClick + it.onPOIClick = onPOIClick + } + val currentLocationSource by rememberUpdatedState(locationSource) + val currentCameraPositionState by rememberUpdatedState(cameraPositionState) + val currentContentPadding by rememberUpdatedState(contentPadding) + val currentUiSettings by rememberUpdatedState(uiSettings) + val currentMapProperties by rememberUpdatedState(properties) - val parentComposition = rememberCompositionContext() - val currentContent by rememberUpdatedState(content) + val parentComposition = rememberCompositionContext() + val currentContent by rememberUpdatedState(content) - LaunchedEffect(Unit) { - disposingComposition { - mapView.newComposition(parentComposition) { - MapUpdater( - contentDescription = contentDescription, - cameraPositionState = currentCameraPositionState, - clickListeners = mapClickListeners, - contentPadding = currentContentPadding, - locationSource = currentLocationSource, - mapProperties = currentMapProperties, - mapUiSettings = currentUiSettings, - ) - CompositionLocalProvider( - LocalCameraPositionState provides cameraPositionState, - ) { - currentContent?.invoke() + LaunchedEffect(Unit) { + disposingComposition { + mapView.newComposition(parentComposition) { + MapUpdater( + contentDescription = contentDescription, + cameraPositionState = currentCameraPositionState, + clickListeners = mapClickListeners, + contentPadding = currentContentPadding, + locationSource = currentLocationSource, + mapProperties = currentMapProperties, + mapUiSettings = currentUiSettings, + ) + CompositionLocalProvider( + LocalCameraPositionState provides cameraPositionState, + ) { + currentContent?.invoke() + } } } } } + } internal suspend inline fun disposingComposition(factory: () -> Composition) { @@ -204,6 +208,7 @@ private fun MapView.lifecycleObserver(previousState: MutableState this.onStart() Lifecycle.Event.ON_RESUME -> this.onResume() Lifecycle.Event.ON_PAUSE -> this.onPause() @@ -211,6 +216,7 @@ private fun MapView.lifecycleObserver(previousState: MutableState { //handled in onDispose } + else -> throw IllegalStateException() } previousState.value = event