Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add onClusterManager to Clustering to allow easy default overrides (e.g. setting minClusterSize) #473

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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