如何在使用Jetpack Composite的地图上刷新集群标记?

3

在我的应用程序中,我展示了一个地图,并使用聚类标记了不同的区域,以便将最近的位置分组。但是我有一个问题。当我修改一些标记(例如使用名称搜索过滤器)时,标记并没有总是被更新。有时新的标记会被绘制,但几乎总是旧的标记还留在那里。我尝试了在重新添加标记之前清除它们,但这并不起作用。这是我的代码:

@Composable
fun MyMap(
    locations: State<LocationData>
) {

    lateinit var clusterManager: ClusterManager<LocationClusterItem>
    val mapView = rememberMapViewWithLifeCycle()
    val localContext = LocalContext.current
    
    Column(
        modifier = Modifier
            .background(Color.White)
    ) {
        AndroidView(
            {
                mapView
            }
        ) {

            mapView.getMapAsync {
                val map = it
                map.uiSettings.isZoomControlsEnabled = false
                clusterManager = ClusterManager(localContext, map)

                clusterManager.clearItems()
                clusterManager.cluster()

                map.setOnCameraIdleListener(clusterManager)
                map.setOnMarkerClickListener(clusterManager)

                for (location in locations.value.sites) {
                    clusterManager.addItem(IncidentClusterItem(location.lat, location.lon, location.street, location.info))
                }

                clusterManager.cluster()

                map.moveCamera(
                    CameraUpdateFactory.newLatLngZoom(
                        LatLng(50.00000000000000, -10.000000000000000),
                        12f
                    )
                )
                map.mapType = GoogleMap.MAP_TYPE_HYBRID
            }

        }
    }

}

怎样才能正确更新地图上的标记?

2
我知道这并不回答你的问题,但是如果你错过了,Google刚刚支持了Compose的地图:https://github.com/googlemaps/android-maps-compose - Stephen Vinouze
哇,我不知道这个!谢谢!我要试试看,也许它会让我更容易地将我的应用程序与地图集成。我唯一没有看到的是它是否支持聚类(将附近的标记分组)。你知道这是否可能吗? - Paul9999
可能还没有得到支持,我还没有在我的一面进一步查看。但这是非常近期的事情,事情正在朝着好的方向发展。 - Stephen Vinouze
我正在尝试使用新的地图Compose库实现相同的功能,但看起来API中没有提供任何刷新标记的方法。 - Korbin
急需为组合地图提供集群支持。 - Renz Carlo
@Paul9999 你解决了这个问题吗? - c_idle
2个回答

0

猜测一下,我认为在mapView.getMapAsync(callback)内读取的状态不会被跟踪。是什么驱动了更新调用?

当标记改变时,您能否从外部更新状态,然后在调用mapViuw.getAsync之前从更新lambda中读取它们?

具体来说,从文档中

AndroidView(
    modifier = Modifier.fillMaxSize(), // Occupy the max size in the Compose UI tree
    factory = { context ->
        // Creates custom view
        CustomView(context).apply {
            // Sets up listeners for View -> Compose communication
            myView.setOnClickListener {
                selectedItem.value = 1
            }
        }
    },
    update = { view ->
        // View's been inflated or state read in this block has been updated
        // Add logic here if necessary

        // As selectedItem is read here, AndroidView will recompose
        // whenever the state changes
        // Example of Compose -> View communication
        view.coordinator.selectedItem = selectedItem.value
    }

我觉得状态读取需要直接在更新块中进行。


似乎我不能在mapViuw.getAsync之前调用for (location in locations.value.sites) { clusterManager.addItem(IncidentClusterItem(location.lat, location.lon, location.street, location.info)) },因为mapViuw.getAsync实际上是设置聚类的。 - c_idle
你之前读过这些位置吗?val locationsRead = locations.value所以它直接在更新块中读取。 - Yuri Schimke

0
为了使您的集群标记列表可观察,您必须在列表上调用 toList()。这与 clusterManager 访问列表的方式有关,而 toList 是正确的方法。
更新: 看起来 compose-maps 将通过一种解决方法支持聚类,如果您想远离 AndroidView 的话。https://github.com/googlemaps/android-maps-compose/pull/140

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接