I'm building a Jetpack Compose app that displays a map with various points of interest. I'd also like to cluster them based on their category (f.ex. restaurants, shops, tourist attractions). The "general" clustering of the points of interest works fine, but I'm having trouble clustering by category.
This is my Composable:
fun MapViewContainer(
mapView: MapView,
points: List<PointOfInterest>
) {
val localContext = LocalContext.current
val coroutineScope = rememberCoroutineScope()
AndroidView({ mapView }) { map ->
coroutineScope.launch {
map.awaitMap().apply {
val clusterManager = ClusterManager<PoiItem>(localContext, this)
this.setOnCameraIdleListener(clusterManager)
clusterManager.renderer = PoiClusterRenderer(localContext, this, clusterManager)
clusterManager.addItems(points)
}
}
}
}
PoiItem.kt
class PoiItem(
lat: Double,
lng: Double,
private val title: String,
private val category: Category,
private val snippet: String
) : ClusterItem {
private val position: LatLng = LatLng(lat, lng)
override fun getPosition(): LatLng = position
override fun getTitle(): String = title
override fun getSnippet(): String = snippet
}
PoiClusterRenderer.kt (to use custom icon)
class PoiClusterRenderer(
private val context: Context,
map: GoogleMap,
clusterManager: ClusterManager<PoiItem>
) : DefaultClusterRenderer<PoiItem>(context, map, clusterManager) {
override fun onBeforeClusterItemRendered(
item: PoiItem,
markerOptions: MarkerOptions
) {
val icon = bitmapDescriptorFromVector(
context = context,
vectorResId = R.drawable.ic_circle,
category = item.category
)
markerOptions.icon(icon)
super.onBeforeClusterItemRendered(item, markerOptions)
}
}
I've already tried using multiple cluster managers but it didn't help. The map was still clustering all of the points of interest, but not by category. Example:
val clusterManagerRestaurants = ClusterManager<PoiItem>(localContext, this)
val clusterManagerShops = ClusterManager<PoiItem>(localContext, this)
clusterManagerRestaurants.renderer = PoiClusterRenderer(localContext, this, clusterManagerRestaurants)
clusterManagerShops.renderer = PoiClusterRenderer(localContext, this, clusterManagerShops)
clusterManagerRestaurants.addItems(points.filter { it.category == "restaurant" })
clusterManagerShops.addItems(points.filter { it.category == "shop" })
setOnCameraIdleListener {
clusterManagerRestaurants.cluster()
clusterManagerShops.cluster()
}
Any help would be greatly appreciated!