0

我正在构建一个 Jetpack Compose 应用程序,该应用程序显示具有各种兴趣点的地图。我还想根据它们的类别(例如餐馆、商店、旅游景点)对它们进行聚类。兴趣点的“一般”聚类工作正常,但我无法按类别聚类。

这是我的可组合:

fun MapViewContainer(
    mapView: MapView,
    points: List<PointOfInterest>
) {
    val localContext = LocalContext.current

    val coroutineScope = rememberCoroutineScope()

    AndroidView({ mapView }) { map ->
        coroutineScope.launch {
            map.awaitMap().apply {
                val clusterManager = ClusterManager<PoiItem>(localContext, this)

                this.setOnCameraIdleListener(clusterManager)

                clusterManager.renderer = PoiClusterRenderer(localContext, this, clusterManager)

                clusterManager.addItems(points)
            }
        }
    }
}

PoiItem.kt

class PoiItem(
    lat: Double,
    lng: Double,
    private val title: String,
    private val category: Category,
    private val snippet: String
) : ClusterItem {

    private val position: LatLng = LatLng(lat, lng)

    override fun getPosition(): LatLng = position

    override fun getTitle(): String = title

    override fun getSnippet(): String = snippet
}

PoiClusterRenderer.kt(使用自定义图标)

class PoiClusterRenderer(
    private val context: Context,
    map: GoogleMap,
    clusterManager: ClusterManager<PoiItem>
) : DefaultClusterRenderer<PoiItem>(context, map, clusterManager) {

    override fun onBeforeClusterItemRendered(
        item: PoiItem,
        markerOptions: MarkerOptions
    ) {
        val icon = bitmapDescriptorFromVector(
            context = context,
            vectorResId = R.drawable.ic_circle,
            category = item.category
        )
        markerOptions.icon(icon)
        super.onBeforeClusterItemRendered(item, markerOptions)
    }
}

我已经尝试过使用多个集群管理器,但没有帮助。该地图仍在对所有兴趣点进行聚类,但不是按类别。例子:

val clusterManagerRestaurants = ClusterManager<PoiItem>(localContext, this)
val clusterManagerShops = ClusterManager<PoiItem>(localContext, this)

clusterManagerRestaurants.renderer = PoiClusterRenderer(localContext, this, clusterManagerRestaurants)
clusterManagerShops.renderer = PoiClusterRenderer(localContext, this, clusterManagerShops)

clusterManagerRestaurants.addItems(points.filter { it.category == "restaurant" })
clusterManagerShops.addItems(points.filter { it.category == "shop" })

setOnCameraIdleListener {
   clusterManagerRestaurants.cluster()
   clusterManagerShops.cluster()
}

任何帮助将不胜感激!

4

0 回答 0