我有一个屏幕,其中包含带有聚类的自定义标记列表,并且工作正常。问题是在打开屏幕后的某个时间,我的谷歌地图显示我的海关标记和同一位置的默认标记。
@Composable
fun MapScreen(stepResult: HomeState.StepResult) {
val sheetState = rememberModalBottomSheetState(
initialValue = ModalBottomSheetValue.Hidden
)
stepResult.games.let { games ->
val items = games.map {
MapTenant(
itemPosition = LatLng(it.latLng.latitude, it.latLng.longitude),
itemTitle = it.name,
itemSnippet = it.address,
itemZIndex = 0f,
item = it
)
}
if (items.isEmpty()) {
return
}
Box {
TenantMapList(sheetState, items)
}
}
}
@Composable
fun GoogleMapClustering(items: List<MapTenant>, onMapSelected: (List<MapTenant>) -> Unit) {
var cameraState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(items.first().itemPosition, 10f)
}
GoogleMap(
modifier = Modifier
.fillMaxSize()
.padding(top = 32.dp),
cameraPositionState = cameraState
) {
CustomUiClustering(items = items, onMapSelected = {
onMapSelected(it)
}, onItemSelected = {
onMapSelected(listOf(it))
})
}
}
@OptIn(MapsComposeExperimentalApi::class)
@Composable
private fun CustomUiClustering(
items: List<MapTenant>,
onMapSelected: (List<MapTenant>) -> Unit,
onItemSelected: (MapTenant) -> Unit
) {
Clustering(items = items,
onClusterClick = {
onMapSelected(it.items.toList())
false
}, onClusterItemClick = {
onItemSelected(it)
true
},
clusterContent = { cluster ->
CircleContent(
modifier = Modifier.size(40.dp),
text = "%,d".format(cluster.size),
)
},
clusterItemContent = {
CircleContent(
modifier = Modifier.size(20.dp),
text = "",
)
})
}
@Composable
private fun CircleContent(
text: String,
modifier: Modifier = Modifier,
) {
Surface(
modifier,
shape = CircleShape,
color = ComplementaryPopUp,
contentColor = Color.White,
border = BorderStroke(2.dp, defaultGradient)
) {
Box(contentAlignment = Alignment.Center) {
Text(
text, fontSize = 16.sp, fontWeight = FontWeight.Black, textAlign = TextAlign.Center
)
}
}
}
@Composable
fun TenantMapList(
sheetState: ModalBottomSheetState,
items: List<MapTenant>,
) {
var selectedTenants by remember { mutableStateOf<List<MapTenant>>(emptyList()) }
ModalBottomSheetLayout(sheetState = sheetState,
sheetShape = RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp),
sheetBackgroundColor = ComplementaryPopUp,
scrimColor = MaterialTheme.colors.onSurface.copy(alpha = 0.50f),
sheetContent = {
TenantsList(Modifier.wrapContentHeight(), selectedTenants)
}) {
val scope = rememberCoroutineScope()
GoogleMapClustering(items) {
selectedTenants = it
scope.launch {
sheetState.show()
}
}
}
}
只是随机的,有时是第一次打开屏幕时,有时是5次后打开屏幕时。
您应该将库更新到版本 6.2.1,应按照此处所述修复此特定错误:https://github.com/googlemaps/android-maps-compose/issues/569#issuecomment-2457775417