Skip to content

Clustering : Markers are not updated #269

@InsideApp-OBO

Description

@InsideApp-OBO

Environment details

Library version: Tested with Maps Compose 2.11.0

Steps to reproduce (List with the same reference)

  1. Display N markers on the map with Clustering composable.
  2. Change the content of the list with totally different markers.
  3. Markers are not updated due to
    LaunchedEffect(items) {
        clusterManager.clearItems()
        clusterManager.addItems(items)
    }

Steps to reproduce (With a new list)

  1. Display N markers on the map with Clustering composable.
  2. Change the content of the list with totally different markers.
  3. Make a copy of the list with toList() function
  4. Markers are updated only when the zoom level changes.

Code example

@OptIn(MapsComposeExperimentalApi::class)
@Composable
fun GoogleMapClustering() {

    val items = remember { mutableStateListOf<MyItem>() }

    val cameraPositionState = rememberCameraPositionState {
        position = CameraPosition.fromLatLngZoom(singapore, 8f)
    }

    LaunchedEffect(cameraPositionState) {
        snapshotFlow { cameraPositionState.isMoving }
            .collect { isMoving ->
                if (!isMoving) {
                    items.apply {
                        clear()
                        for (i in 1..10) {
                            val position = LatLng(
                                singapore.latitude + Random.nextFloat(),
                                singapore.longitude + Random.nextFloat(),
                            )
                            add(MyItem(position, "Marker $i", "Snippet"))
                        }
                    }
                }
            }
    }

    Box(modifier = Modifier.fillMaxSize()) {
        GoogleMap(
            modifier = Modifier.fillMaxSize(),
            cameraPositionState = cameraPositionState
        ) {
            Clustering(
                items = items//.toList() // Force new list to avoid Clustering LaunchedEffect(items)
            )
        }
        Text(text = "Items : ${items.map { it.itemPosition }}")
    }
}

How to dynamically update the markers when the map is moved ?
(Ex : API Rest call with ViewModel)

Thank you for all your work !

Metadata

Metadata

Assignees

Labels

priority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.releasedtype: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions