0

We have developed an application using Compose and using version 5.0.1 for the map screen and to add markers.

After moving and stopping, we make a request to the server and receive a response with a list of markers. We map these to the UI model and try to draw them on the map as shown below:


GoogleMap(
    modifier = modifier,
    cameraPositionState = cameraPositionState,
    properties = mapProperties,
    uiSettings = uiSettings,
    onMapClick = { onMapClick() },
    onMapLoaded = {
        onMapLoaded(
            LocationLatLng(
                cameraPositionState.position.target.latitude,
                cameraPositionState.position.target.longitude
            ),
            cameraPositionState.visibleAreaAsRadius(defaultVisibleAreaDistance)
        )
    }
) {
      markers?.forEach { marker ->
          MarkerComposable(
              state = rememberMarkerState(
                  key = marker.key,
                  position = LatLng(
                      marker.position.latitude,
                      marker.position.longitude
                  ),
              ),
              onClick = {
                  marker.onMarkerClick()
                  return@MarkerComposable false
              }
          ) {
              Image(
                  modifier = Modifier.size(64.dp),
                  painter = painterResource(marker.iconResId),
                  contentDescription = marker.contentDescriptionResId?.let { stringResource(it) },
              )
          }
      }
  }
...
}

However, I sometimes notice that even if the markers are in the list, they do not get drawn on the map screen. What could be the reason for this?

Thanks in advance.

Note: Marker keys are unique.

I would expect that markers appearing on the map are displayed on the map screen as I move it. The marker does not appear after entering rememberMarkerState, it doesn't recompose.

Screen record

2
  • 1
    Your link to the screen recording seems not to be working for me. Also, can you edit you post and show where the markers list is defined & initialized with the data?
    – BenjyTec
    Commented Jun 7 at 10:25
  • I've updated this with the video I uploaded to YouTube. Commented Jun 7 at 11:37

1 Answer 1

0

You can always start with the "Add a map to your Android app codelab": https://developers.google.com/codelabs/maps-platform/maps-platform-101-compose#0

It is important that the list of markers is observable. It is difficult to tell from the code snippet how the list is changing.

In the codelab, the mountainsScreenViewState is observable (it is a hot StateFlow). This ensures the map is recomposed when the list of markers changes (for example, with the user toggles between the full and filtered list). Markers are added in BasicMarkersMapContent. Which is added to the GoogleMap composable here.

If this does not help, please add more detail about the data backing your list of markers. A complete, small example would be helpful in reproducing the problem that you are experiencing.

Not the answer you're looking for? Browse other questions tagged or ask your own question.