Last active
November 20, 2023 09:25
-
-
Save bmc08gt/044e9c1c715d7b87c453025733e53c6e to your computer and use it in GitHub Desktop.
Compose Map Markers Not Clearing Fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) | |
) | |
} | |
} | |
[...] | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+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