You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When an animation occurs and is cancelled (and in other circumstances that I did not manage to reproduce), the CameraPositionState gets corrupted and a drag gesture on the map doesn't work anymore as intended. Here is a code to reproduce the bug (click several times on the button and try to move the map, zoom is not affected somehow).
Environment details
android-maps-compose 4.3.0
doesn't seem to occur on the simulator, please use a real device.
Steps to reproduce
Click several times on the button that animates the card, causing cancellation exceptions (caught and displayed)
Try to move the map, it doesn't move as intended (zoom is not affected somehow)
Code example
class RecompositionActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val cameraPositionState = rememberCameraPositionState {
position = defaultCameraPosition
}
Box(Modifier.fillMaxSize()) {
MapsComposeSampleTheme {
GoogleMapView(
modifier = Modifier.matchParentSize(),
cameraPositionState = cameraPositionState
)
}
}
}
}
@Composable
fun GoogleMapView(
modifier: Modifier = Modifier,
cameraPositionState: CameraPositionState = rememberCameraPositionState(),
content: @Composable () -> Unit = {},
) {
var location by remember { mutableStateOf(singapore) }
var zoom by remember { mutableStateOf(5f) }
val context = LocalContext.current
val singaporeState = MarkerState(position = location)
val mapProperties = remember {
MapProperties(
isBuildingEnabled = false,
isIndoorEnabled = false,
isMyLocationEnabled = false,
isTrafficEnabled = false,
mapType = MapType.SATELLITE,
)
}
val mapOptions = remember {
GoogleMapOptions()
.ambientEnabled(false)
.mapToolbarEnabled(false)
.compassEnabled(false)
}
val uiSettings = remember {
MapUiSettings(
compassEnabled = false,
indoorLevelPickerEnabled = false,
mapToolbarEnabled = false,
myLocationButtonEnabled = false,
rotationGesturesEnabled = false,
scrollGesturesEnabledDuringRotateOrZoom = false,
tiltGesturesEnabled = false,
zoomControlsEnabled = false,
)
}
val mapVisible by remember { mutableStateOf(true) }
if (mapVisible) {
LaunchedEffect(zoom) {
try {
cameraPositionState.animate(
update = CameraUpdateFactory.newCameraPosition(
CameraPosition(
cameraPositionState.position.target,
zoom,
0f,
0f
),
), durationMs = 1000
)
} catch(e: CancellationException) {
Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
} catch(e: Exception) {
Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
}
}
// Detect when the map starts moving and print the reason
LaunchedEffect(cameraPositionState.isMoving) {
if (cameraPositionState.isMoving) {
Log.d(TAG, "Map camera started moving due to ${cameraPositionState.cameraMoveStartedReason.name}")
}
}
GoogleMap(
modifier = modifier.fillMaxSize(),
cameraPositionState = cameraPositionState,
googleMapOptionsFactory = {mapOptions},
onMapLoaded = {},
properties = mapProperties,
uiSettings = uiSettings,
onPOIClick = {
Log.d(TAG, "POI clicked: ${it.name}")
}
) {
val markerClick: (Marker) -> Boolean = {
Log.d(TAG, "${it.title} was clicked")
cameraPositionState.projection?.let { projection ->
Log.d(TAG, "The current projection is: $projection")
}
false
}
Marker(
state = singaporeState,
title = "Marker in Singapore",
onClick = markerClick
)
content()
}
Column {
Button(onClick = {
val randomValue = Random.nextInt(3)
location = when (randomValue) {
0 -> singapore
1 -> singapore2
2 -> singapore3
else -> singapore
}
zoom = when (randomValue) {
0 -> 3f
1 -> 6f
2 -> 15f
else -> 16f
}
}) {
Text("Change Location & Zoom")
}
}
}
}
}
Thanks by advance!
The text was updated successfully, but these errors were encountered:
jeffphp
added
triage me
I really want to be triaged.
type: bug
Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
labels
Jan 5, 2024
If you would like to upvote the priority of this issue, please comment below or react on the original post above with 👍 so we can see what is popular when we triage.
@jeffphp Thank you for opening this issue. 🙏
Please check out these other resources that might help you get to a resolution in the meantime:
A workaround fixing some problems consists in catching CancellationException and throw it again when it happens, then there won't be a discrepancy between cameraPositionState and drag gesture listener :
This function should not be used to (re-)launch ongoing tasks in response to callback events by way of storing callback data in MutableState passed to key. Instead, see rememberCoroutineScope to obtain a CoroutineScope that may be used to launch ongoing jobs scoped to the composition in response to event callbacks.
android-maps-compose 4.3.0
When an animation occurs and is cancelled (and in other circumstances that I did not manage to reproduce), the CameraPositionState gets corrupted and a drag gesture on the map doesn't work anymore as intended. Here is a code to reproduce the bug (click several times on the button and try to move the map, zoom is not affected somehow).
Environment details
Steps to reproduce
Code example
Thanks by advance!
The text was updated successfully, but these errors were encountered: