Jetpack Compose GoogleMap:MarkerComposable 抛出异常

问题描述 投票:0回答:1

自从我从 XML 和

GoogleMap
“老派”方式切换以来,我第一次使用
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<MapClusterItem>,
    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
    }
}

完全例外:

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.
                                                                                                        at com.google.maps.android.compose.RememberComposeBitmapDescriptorKt.renderComposableToBitmapDescriptor(RememberComposeBitmapDescriptor.kt:58)
                                                                                                        at com.google.maps.android.compose.RememberComposeBitmapDescriptorKt.access$renderComposableToBitmapDescriptor(RememberComposeBitmapDescriptor.kt:1)
                                                                                                        at com.google.maps.android.compose.RememberComposeBitmapDescriptorKt.rememberComposeBitmapDescriptor(RememberComposeBitmapDescriptor.kt:29)
                                                                                                        at com.google.maps.android.compose.MarkerKt.MarkerComposable-Khg_OnI(Marker.kt:313)
                                                                                                        at com.damidev.ybox24.ui.compose.map.BaseGoogleMapKt.BaseItemMarker(BaseGoogleMap.kt:124)
                                                                                                        at com.damidev.ybox24.ui.compose.map.ComposableSingletons$BaseGoogleMapKt$lambda-2$1.invoke(BaseGoogleMap.kt:100)
                                                                                                        at com.damidev.ybox24.ui.compose.map.ComposableSingletons$BaseGoogleMapKt$lambda-2$1.invoke(BaseGoogleMap.kt:99)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:118)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
                                                                                                        at com.google.maps.android.compose.clustering.ComposeUiClusterRenderer$createAndAddView$view$2.invoke(ClusterRenderer.kt:95)
                                                                                                        at com.google.maps.android.compose.clustering.ComposeUiClusterRenderer$createAndAddView$view$2.invoke(ClusterRenderer.kt:95)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
                                                                                                        at com.google.maps.android.compose.clustering.ComposeUiClusterRenderer$InvalidatingComposeView.Content(ClusterRenderer.kt:220)
                                                                                                        at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:259)
                                                                                                        at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:258)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
                                                                                                        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:380)
                                                                                                        at androidx.compose.ui.platform.CompositionLocalsKt.ProvideCommonCompositionLocals(CompositionLocals.kt:216)
                                                                                                        at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:132)
                                                                                                        at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:131)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
                                                                                                        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:380)
                                                                                                        at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt.ProvideAndroidCompositionLocals(AndroidCompositionLocals.android.kt:121)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$3.invoke(Wrapper.android.kt:155)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$3.invoke(Wrapper.android.kt:154)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
                                                                                                        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:401)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:154)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:133)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
android android-jetpack-compose google-maps-markers
1个回答
0
投票

您尚未在帖子中包含代码,因此请检查您的

BaseItemMarker
可组合项。
堆栈跟踪显示其中似乎没有指定
size
。您可以尝试应用
size
修改器,或者将其完全删除,看看是否可以解决问题:

Clustering(
    //...
    clusterContent = { cluster ->
        BaseClusterMarker(
            size = cluster.size,
            position = cluster.position,
        )
    },
    // delete clusterItemContent parameter
}
© www.soinside.com 2019 - 2024. All rights reserved.