Я работаю с компонуемым GoogleMap впервые с тех пор, как перешел с XML и ClusterRenderer «старым школьным» способом.
Но я создал ClusterMarker, который должен отображать булавку маркера и размер этого кластера в виде числа внутри этого значка булавки.
Это мой элемент:
@Composable
fun BaseClusterMarker(
size: Int,
position: LatLng,
) {
val markerState = rememberMarkerState(position = position)
MarkerComposable(
state = markerState,
content = {
Box(
modifier = Modifier
.size(
width = 40.dp,
height = 47.dp,
),
contentAlignment = Alignment.Center,
) {
Image(
modifier = Modifier
.matchParentSize(),
painter = painterResource(id = R.drawable.map_pin),
contentDescription = "",
)
Text(
modifier = Modifier
.align(Alignment.Center),
color = MainTheme.colors.main.mapColors.pinTextColor,
text = size.toString(),
style = MainTheme.typography.text.medium.bold,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
textAlign = TextAlign.Center,
)
}
},
)
}
Но у меня почему-то получается
java.lang.IllegalStateException: The ComposeView was measured to have a width or height of zero. Make sure that the content has a non-zero size.
Как MarkerComposable выдает это исключение, если Box имеет фиксированный размер?
Компонуемая полная карта:
@OptIn(MapsComposeExperimentalApi::class)
@Composable
fun BaseGoogleMap(
modifier: Modifier = Modifier,
defaultPosition: LatLng = Config.GoogleMap.getDefaultPosition(),
pins: List,
onPinClick: (MapClusterItem) -> Unit,
) {
val context = LocalContext.current
val cameraPositionState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(defaultPosition, Config.GoogleMap.DEFAULT_ZOOM_LEVEL)
}
val mapStyleOptions = remember {
loadMapStyleOptions(context, R.raw.custommapstyle)
}
val coroutineScope = remember { CoroutineScope(Dispatchers.Main) }
GoogleMap(
modifier = modifier
.fillMaxSize(),
cameraPositionState = cameraPositionState,
uiSettings = MapUiSettings(zoomControlsEnabled = false),
properties = MapProperties(
mapStyleOptions = mapStyleOptions,
),
) {
Clustering(
items = pins,
onClusterClick = { cluster ->
coroutineScope.launch {
cameraPositionState.animate(
CameraUpdateFactory.newLatLngZoom(
cluster.position,
cameraPositionState.position.zoom + 2
),
1000
)
}
true
},
onClusterItemClick = { item ->
onPinClick.invoke(item)
false
},
clusterContent = { cluster ->
BaseClusterMarker(
size = cluster.size,
position = cluster.position,
)
},
clusterItemContent = { item ->
BaseItemMarker(
position = item.position,
)
},
)
}
}
// Helper function to load MapStyleOptions from a JSON file
fun loadMapStyleOptions(context: Context, styleResId: Int): MapStyleOptions? {
return try {
val styleJson = context.resources.openRawResource(styleResId).bufferedReader().use { it.readText() }
MapStyleOptions(styleJson)
} catch (e: Exception) {
e.printStackTrace()
null // Return null if there is an issue loading the style
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... -exception
Jetpack Compose GoogleMap: MarkerComposable выдает исключение ⇐ Android
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Отображение LazyRow ElevatedCards внизу карты GoogleMap в Jetpack Compose
Anonymous » » в форуме Android - 0 Ответы
- 25 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Как панорамировать компонуемый GoogleMap внутри ModalBottomSheet в JetPack Compose
Anonymous » » в форуме Android - 0 Ответы
- 11 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Как панорамировать компонуемый GoogleMap внутри ModalBottomSheet в JetPack Compose
Anonymous » » в форуме Android - 0 Ответы
- 17 Просмотры
-
Последнее сообщение Anonymous
-