Skip to content

Instantly share code, notes, and snippets.

@bmc08gt
Last active November 20, 2023 09:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmc08gt/044e9c1c715d7b87c453025733e53c6e to your computer and use it in GitHub Desktop.
Save bmc08gt/044e9c1c715d7b87c453025733e53c6e to your computer and use it in GitHub Desktop.
Compose Map Markers Not Clearing Fix
@Composable
actual fun MapView(
modifier: Modifier,
contentPadding: PaddingValues,
userLocation: LatLong?,
resultLocations: List<Stay.Minimal>,
useTotalPrice: Boolean,
onMarkerSelectionChange: (String?) -> Unit,
onMapMoved: () -> Unit,
) {
[...]
BoxWithConstraints(modifier = modifier) {
GoogleMap(
modifier = Modifier.matchParentSize(),
contentPadding = contentPadding,
contentDescription = "Map view for properties with applied filters and search parameters",
cameraPositionState = cameraPositionState,
properties = properties,
uiSettings = uiSettings,
onMapClick = { currentSelectedMarkerId = null }
) {
resultLocations.forEach { listing ->
val location = LatLng(listing.location.latitude, listing.location.longitude)
val markerState = rememberMarkerState(
position = location
)
MarkerComposable(
keys = arrayOf(
listing.id,
location,
useTotalPrice,
currentSelectedMarkerId ?: "",
previousSelectedMarkers
),
state = markerState,
tag = listing.id,
onClick = {
currentSelectedMarkerId = it.tag as? String
true
}
) {
PricePill(
price = "${listing.totalPriceOfStay()}".formatAsMoney(),
isSelected = currentSelectedMarkerId == listing.id,
wasPreviouslySelected = previousSelectedMarkers.contains(listing.id)
)
}
}
[...]
}
}
}
+var markersToDraw by remember(resultLocations) {
+ mutableStateOf(emptyList<Stay.Minimal>())
+}
GoogleMap(
modifier = Modifier.matchParentSize(),
contentPadding = contentPadding,
contentDescription = "Map view for properties with applied filters and search parameters",
cameraPositionState = cameraPositionState,
properties = properties,
uiSettings = uiSettings,
onMapClick = { currentSelectedMarkerId = null }
) {
+ markersToDraw.forEach { listing ->
val location = LatLng(listing.location.latitude, listing.location.longitude)
val markerState = rememberMarkerState(
position = location
)
MarkerComposable(
keys = arrayOf(
listing.id,
location,
useTotalPrice,
currentSelectedMarkerId ?: "",
previousSelectedMarkers
),
state = markerState,
tag = listing.id,
onClick = {
currentSelectedMarkerId = it.tag as? String
true
}
) {
PricePill(
price = "${listing.totalPriceOfStay()}".formatAsMoney(),
isSelected = currentSelectedMarkerId == listing.id,
wasPreviouslySelected = previousSelectedMarkers.contains(listing.id)
)
}
}
[...]
+ LaunchedEffect(resultLocations) {
+ markersToDraw = resultLocations
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment