---
title: "MapboxClusterLayer"
description: "点聚合图层，基于 geojson cluster 自动渲染聚合圆、计数与散点三层，点击聚合圆展开。"
seo_title: "MapboxClusterLayer Component"
seo_description: "MapboxClusterLayer renders clustered points with a geojson cluster source as three layers (cluster circles, counts, unclustered points) and expands on click."
canonical_url: "https://mapbox.mhaibaraai.cn/docs/layers/cluster"
---
# MapboxClusterLayer

> 点聚合图层，基于 geojson cluster 自动渲染聚合圆、计数与散点三层，点击聚合圆展开。

## 简介

`MapboxClusterLayer` 把大量点数据自动聚合：内部用开启 `cluster` 的 geojson 源渲染「聚合圆 + 计数文字 + 散点」三层。点击聚合圆默认放大到展开级别（`autoExpand`），并派发 `clusterClick` / `pointClick` 事件。

## 用法

传入点数据即自动聚合，点击聚合圆展开：

```vue [ClusterLayerExample.vue]
<script setup lang="ts">
import type { FeatureCollection } from 'geojson'

const data: FeatureCollection = {
  type: 'FeatureCollection',
  features: Array.from({ length: 150 }, (_, i) => ({
    type: 'Feature',
    properties: {},
    geometry: {
      type: 'Point',
      coordinates: [116.0 + ((i * 37) % 100) / 125, 39.7 + ((i * 53) % 100) / 167]
    }
  }))
}
</script>

<template>
  <div class="h-115 w-full overflow-hidden rounded-(--ui-radius) border border-default">
    <MapboxMap :options="{ style: 'mapbox://styles/mapbox/light-v11', center: [116.4, 39.9], zoom: 9 }">
      <!-- 自动聚合：点击聚合圆放大展开，散点单独渲染 -->
      <MapboxClusterLayer :data="data" />
    </MapboxMap>
  </div>
</template>
```

## API

### Props

```ts
/**
 * Props for the MapboxClusterLayer component
 */
interface MapboxClusterLayerProps {
  /**
   * 点要素数据（GeoJSON 或其 URL）
   */
  data: string | GeoJSON.GeoJSON<GeoJSON.Geometry, GeoJSON.GeoJsonProperties> | undefined;
  /**
   * source 与图层 id 前缀；省略时自动生成
   */
  sourceId?: string | undefined;
  /**
   * 聚合半径（像素）
   * @default "50"
   */
  clusterRadius?: number | undefined;
  /**
   * 超过该缩放级别不再聚合
   * @default "14"
   */
  clusterMaxZoom?: number | undefined;
  /**
   * 覆盖聚合圆 paint
   */
  clusterPaint?: PropBag | undefined;
  /**
   * 覆盖计数文字 layout
   */
  countLayout?: PropBag | undefined;
  /**
   * 覆盖计数文字 paint
   */
  countPaint?: PropBag | undefined;
  /**
   * 覆盖散点 paint
   */
  pointPaint?: PropBag | undefined;
  /**
   * 点击聚合圆自动放大到展开级别
   * @default "true"
   */
  autoExpand?: boolean | undefined;
  /**
   * 插入到该图层之前
   */
  beforeId?: string | undefined;
}
```

### Emits

```ts
/**
 * Emitted events for the MapboxClusterLayer component
 */
interface MapboxClusterLayerEmits {
  clusterClick: (payload: [payload: { clusterId: number; coordinates: [number, number]; expansionZoom: number; }]) => void;
  pointClick: (payload: [feature: mapboxgl.GeoJSONFeature]) => void;
}
```

## Changelog

See commit history for [src/runtime/components/layers/ClusterLayer.vue](https://github.com/mhaibaraai/movk-mapbox/commits/main/src/runtime/components/layers/ClusterLayer.vue).


## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
