Wednesday, 25 May 2022

How to refresh cluster markers on a map with Jetpack Composite?

In my application I am showing a map with different zones marked using the clusters so that the closest locations are grouped. But I have a problem. When I modify some marker (using for example search filters by name) the markers are not always updated. Sometimes the new ones are painted, but almost always the old ones stay. I've tried clearing the markers before adding them back, but it doesn't work. This is my code:

@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
            }

        }
    }

}

How can I make the markers on the map update correctly?



from How to refresh cluster markers on a map with Jetpack Composite?

No comments:

Post a Comment