From 2caec8c46f15f4926b9997f53d398950b0493f19 Mon Sep 17 00:00:00 2001 From: darronschall Date: Tue, 12 Dec 2023 10:54:05 -0500 Subject: [PATCH] feat: Add `onClusterManager` hook This allows callers to easily access the default `clusterManager` and renderer that the library sets up, allowing calls to to configure e.g. `minClusterSize`. --- .../MarkerClusteringActivity.kt | 7 +++- .../android/compose/clustering/Clustering.kt | 42 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/google/maps/android/compose/markerexamples/MarkerClusteringActivity.kt b/app/src/main/java/com/google/maps/android/compose/markerexamples/MarkerClusteringActivity.kt index 8301b006..2a874dab 100644 --- a/app/src/main/java/com/google/maps/android/compose/markerexamples/MarkerClusteringActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/markerexamples/MarkerClusteringActivity.kt @@ -40,6 +40,7 @@ import com.google.android.gms.maps.model.CameraPosition import com.google.android.gms.maps.model.LatLng import com.google.maps.android.clustering.ClusterItem import com.google.maps.android.clustering.algo.NonHierarchicalViewBasedAlgorithm +import com.google.maps.android.clustering.view.DefaultClusterRenderer import com.google.maps.android.compose.GoogleMap import com.google.maps.android.compose.MapsComposeExperimentalApi import com.google.maps.android.compose.MarkerInfoWindow @@ -173,7 +174,11 @@ private fun CustomUiClustering(items: List) { ) }, // Optional: Custom rendering for non-clustered items - clusterItemContent = null + clusterItemContent = null, + // Optional: Customization hook for clusterManager and renderer when they're ready + onClusterManager = { clusterManager -> + (clusterManager.renderer as DefaultClusterRenderer).minClusterSize = 2 + }, ) } diff --git a/maps-compose-utils/src/main/java/com/google/maps/android/compose/clustering/Clustering.kt b/maps-compose-utils/src/main/java/com/google/maps/android/compose/clustering/Clustering.kt index dba8bcaa..0cf4bb29 100644 --- a/maps-compose-utils/src/main/java/com/google/maps/android/compose/clustering/Clustering.kt +++ b/maps-compose-utils/src/main/java/com/google/maps/android/compose/clustering/Clustering.kt @@ -125,6 +125,46 @@ public fun Clustering( onClusterItemInfoWindowLongClick: (T) -> Unit = { }, clusterContent: @[UiComposable Composable] ((Cluster) -> Unit)? = null, clusterItemContent: @[UiComposable Composable] ((T) -> Unit)? = null, +) { + Clustering( + items = items, + onClusterClick = onClusterClick, + onClusterItemClick = onClusterItemClick, + onClusterItemInfoWindowClick = onClusterItemInfoWindowClick, + onClusterItemInfoWindowLongClick = onClusterItemInfoWindowLongClick, + clusterContent = clusterContent, + clusterItemContent = clusterItemContent, + onClusterManager = null, + ) +} + +/** + * Groups many items on a map based on zoom level. + * + * @param items all items to show + * @param onClusterClick a lambda invoked when the user clicks a cluster of items + * @param onClusterItemClick a lambda invoked when the user clicks a non-clustered item + * @param onClusterItemInfoWindowClick a lambda invoked when the user clicks the info window of a + * non-clustered item + * @param onClusterItemInfoWindowLongClick a lambda invoked when the user long-clicks the info + * window of a non-clustered item + * @param clusterContent an optional Composable that is rendered for each [Cluster]. + * @param clusterItemContent an optional Composable that is rendered for each non-clustered item. + * @param onClusterManager an optional lambda invoked with the clusterManager as a param when both + * the clusterManager and renderer are set up, allowing callers a customization hook. + */ +@Composable +@GoogleMapComposable +@MapsComposeExperimentalApi +public fun Clustering( + items: Collection, + onClusterClick: (Cluster) -> Boolean = { false }, + onClusterItemClick: (T) -> Boolean = { false }, + onClusterItemInfoWindowClick: (T) -> Unit = { }, + onClusterItemInfoWindowLongClick: (T) -> Unit = { }, + clusterContent: @[UiComposable Composable] ((Cluster) -> Unit)? = null, + clusterItemContent: @[UiComposable Composable] ((T) -> Unit)? = null, + onClusterManager: ((ClusterManager) -> Unit)? = null, ) { val clusterManager = rememberClusterManager() val renderer = rememberClusterRenderer(clusterContent, clusterItemContent, clusterManager) @@ -140,6 +180,8 @@ public fun Clustering( clusterManager.setOnClusterItemClickListener(onClusterItemClick) clusterManager.setOnClusterItemInfoWindowClickListener(onClusterItemInfoWindowClick) clusterManager.setOnClusterItemInfoWindowLongClickListener(onClusterItemInfoWindowLongClick) + + onClusterManager?.invoke(clusterManager) } if (clusterManager != null) {