Skip to content

Marker Clustering

El módulo android-marker-clustering proporciona agrupación automática de marcadores para MapConductor. Agrupa los marcadores cercanos en clústeres al alejar el zoom, y los expande de vuelta en marcadores individuales al acercar el zoom. Funciona con cualquier proveedor de mapas (Google Maps, MapLibre, Mapbox, ArcGIS, HERE, etc.).

dependencies {
implementation(platform("com.mapconductor:mapconductor-bom:1.1.3"))
implementation("com.mapconductor:core")
implementation("com.mapconductor:marker-clustering")
// Choose your map provider
implementation("com.mapconductor:for-googlemaps")
}

Wrap your markers with MarkerClusterGroup inside any XxxMapView content block:

val markerStates: List<MarkerState> = remember { buildMarkerList() }
XxxMapView(...) {
MarkerClusterGroup<ActualMarker>(
state = remember { MarkerClusterGroupState() },
markers = markerStates,
)
}

Replace ActualMarker with the map-specific marker type for your provider (e.g. Marker for Google Maps, Symbol for MapLibre).

Usa MarkerClusterGroupState para configurar el comportamiento del clúster:

val clusterState = remember {
MarkerClusterGroupState<ActualMarker>(
clusterRadiusPx = 50.0,
minClusterSize = 2,
onClusterClick = { cluster ->
// handle cluster tap
},
)
}
XxxMapView(...) {
MarkerClusterGroup(
state = clusterState,
markers = markerStates,
)
}

Provide a clusterIconProvider lambda to render a custom icon for each cluster:

val clusterState = remember {
MarkerClusterGroupState<ActualMarker>(
clusterIconProvider = { count ->
DefaultMarkerIcon(
fillColor = Color.Blue,
label = count.toString(),
labelTextColor = Color.White,
)
},
)
}

Agrega overlays adicionales junto a los marcadores agrupados usando el lambda content:

XxxMapView(...) {
MarkerClusterGroup(
state = clusterState,
markers = markerStates,
) {
// Additional composables rendered alongside the cluster group
Circle(circleState)
}
}
PropertyTypeDefaultDescription
clusterRadiusPxDouble50.0Pixel radius within which markers are clustered
minClusterSizeInt2Minimum number of markers to form a cluster
expandMarginDoubleExtra margin when expanding clusters
clusterIconProvider(Int) -> MarkerIconInterfaceDefault circle badgeReturns an icon for the given cluster size
onClusterClick((MarkerCluster) -> Unit)?nullCalled when a cluster marker is tapped
enableZoomAnimationBooleanfalseAnimate markers on zoom change
enablePanAnimationBooleanfalseAnimate markers on pan
zoomAnimationDurationMillisLongDuration of zoom animation
cameraIdleDebounceMillisLongDebounce delay before re-clustering after camera stops