Skip to main content

Cluster

GmvCluster wraps MarkerClusterer from @googlemaps/markerclusterer. It groups google.maps.marker.AdvancedMarkerElement instances created by GmvMarker.

The component is exported as Cluster from @gmap-vue/v3/components and registered by the plugin as GmvCluster.

Props

Cluster props interface
import type {
Algorithm,
Renderer,
onClusterClickHandler,
} from "@googlemaps/markerclusterer";

export interface ClusterProps {
algorithm?: Algorithm;
markers?: google.maps.marker.AdvancedMarkerElement[];
onClusterClick?: onClusterClickHandler;
renderer?: Renderer;
clusterKey?: string;
mapKey?: string;
options?: Record<string, unknown>;
}
PropTypeDefaultDescription
algorithmAlgorithmundefinedMarkerClusterer algorithm implementation.
markersgoogle.maps.marker.AdvancedMarkerElement[]undefinedInitial markers passed directly to the clusterer. Child GmvMarker components can also add themselves.
onClusterClickonClusterClickHandlerundefinedCustom cluster click handler.
rendererRendererundefinedCustom cluster renderer.
clusterKeystringundefinedPromise key used by useClusterPromise(key) and by markers that join this cluster through cluster-key.
mapKeystringundefinedMap promise key used when the cluster is not a direct descendant of GmvMap.
optionsRecord<string, unknown>undefinedFallback input for supported wrapper fields. The component currently forwards markers, onClusterClick, algorithm, and renderer to MarkerClusterer.

Map and marker resolution

When mapKey is provided, the component injects that map promise. Without mapKey, it finds the parent GmvMap instance.

GmvMarker can join a cluster in two ways:

  1. as a child of GmvCluster, through parent discovery,
  2. with a matching cluster-key, through the cluster promise registry.

Clustered markers are created without a map and then added to the clusterer.

Events

MarkerClusterer emits a small event surface. Use onClusterClick when you need the original map click event together with the clicked cluster and map.

EventPayloadDescription
clusteringbeginMarkerClustererEmitted when MarkerClusterer starts clustering.
clusteringendMarkerClustererEmitted when MarkerClusterer finishes clustering.
clickClusterEmitted when a cluster is clicked.

Exposed properties

PropertyTypeDescription
clusterPromisePromise<MarkerClusterer | undefined>Resolves to the underlying MarkerClusterer instance.
<script setup lang="ts">
import { Cluster } from "@gmap-vue/v3/components";
import { ref } from "vue";

const clusterRef = ref<InstanceType<typeof Cluster> | null>(null);

async function renderClusters() {
const cluster = await clusterRef.value?.clusterPromise;
cluster?.render();
}
</script>

<template>
<GmvMap
:center="{ lat: -34.6037, lng: -58.3816 }"
:zoom="12"
style="width: 100%; height: 500px"
>
<GmvCluster ref="clusterRef">
<GmvMarker :position="{ lat: -34.5889, lng: -58.4306 }" />
</GmvCluster>
</GmvMap>

<button @click="renderClusters">Render clusters</button>
</template>

Composable access

Use useClusterPromise(key?) when direct component refs are inconvenient.

import { useClusterPromise } from "@gmap-vue/v3/composables";

const clusterPromise = useClusterPromise("store-cluster");

Lifecycle behavior

On unmount, GmvCluster clears clustered markers, detaches the clusterer from the map, and destroys the promise key. On component updates, it calls cluster.render().