Skip to content

Commit

Permalink
feat: Add onClusterManager hook
Browse files Browse the repository at this point in the history
This allows callers to easily access the default `clusterManager` and renderer that the library sets up, allowing calls to to configure e.g. `minClusterSize`.
  • Loading branch information
darronschall committed Jul 1, 2024
1 parent f4c97fe commit 2caec8c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -173,7 +174,11 @@ private fun CustomUiClustering(items: List<MyItem>) {
)
},
// 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
},
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,46 @@ public fun <T : ClusterItem> Clustering(
onClusterItemInfoWindowLongClick: (T) -> Unit = { },
clusterContent: @[UiComposable Composable] ((Cluster<T>) -> 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 <T : ClusterItem> Clustering(
items: Collection<T>,
onClusterClick: (Cluster<T>) -> Boolean = { false },
onClusterItemClick: (T) -> Boolean = { false },
onClusterItemInfoWindowClick: (T) -> Unit = { },
onClusterItemInfoWindowLongClick: (T) -> Unit = { },
clusterContent: @[UiComposable Composable] ((Cluster<T>) -> Unit)? = null,
clusterItemContent: @[UiComposable Composable] ((T) -> Unit)? = null,
onClusterManager: ((ClusterManager<T>) -> Unit)? = null,
) {
val clusterManager = rememberClusterManager<T>()
val renderer = rememberClusterRenderer(clusterContent, clusterItemContent, clusterManager)
Expand All @@ -140,6 +180,8 @@ public fun <T : ClusterItem> Clustering(
clusterManager.setOnClusterItemClickListener(onClusterItemClick)
clusterManager.setOnClusterItemInfoWindowClickListener(onClusterItemInfoWindowClick)
clusterManager.setOnClusterItemInfoWindowLongClickListener(onClusterItemInfoWindowLongClick)

onClusterManager?.invoke(clusterManager)
}

if (clusterManager != null) {
Expand Down

0 comments on commit 2caec8c

Please sign in to comment.