聚合与大量点数据展示
🎯 引言
学完本文,你能把密集点按当前缩放级别聚合显示,并在用户点击聚合点时平滑进入下一层级。
🧱 为什么需要聚合
大量点直接显示会互相遮挡,也会增加绘制和交互查询成本。**Cluster(聚合)**会把屏幕上相近的点暂时合成一个圆,并显示其中包含的点数;放大后再逐步拆开。
聚合不会修改原始点坐标,它只是 GeoJSON Source 在当前缩放级别生成的显示结果。
⚙️ 开启 GeoJSON 聚合
map.addSource('places', {
type: 'geojson',
data: placeGeoJSON,
cluster: true,
clusterRadius: 50,
clusterMaxZoom: 14,
})
clusterRadius是聚合判断的像素半径,值越大,附近点越容易合并。clusterMaxZoom是仍然执行聚合的最大缩放级别,继续放大后显示单点。
两个值都需要结合数据密度、地图画幅和业务阅读需求调整。
🎨 分别绘制聚合点与单点
map.addLayer({
id: 'clusters',
type: 'circle',
source: 'places',
filter: ['has', 'point_count'],
paint: {
'circle-color': [
'step', ['get', 'point_count'],
'#60a5fa',
20, '#2563eb',
100, '#1e3a8a',
],
'circle-radius': [
'step', ['get', 'point_count'],
18,
20, 24,
100, 30,
],
},
})
map.addLayer({
id: 'cluster-count',
type: 'symbol',
source: 'places',
filter: ['has', 'point_count'],
layout: {
'text-field': ['get', 'point_count_abbreviated'],
'text-size': 12,
},
paint: { 'text-color': '#ffffff' },
})
map.addLayer({
id: 'unclustered-places',
type: 'circle',
source: 'places',
filter: ['!', ['has', 'point_count']],
paint: { 'circle-radius': 6, 'circle-color': '#16a34a' },
})
point_count 只存在于聚合 Feature,过滤器据此把聚合点和普通点交给不同 Layer。
🔍 点击聚合点放大
map.on('click', 'clusters', (event) => {
const feature = event.features?.[0]
if (!feature || feature.geometry.type !== 'Point') return
const clusterId = Number(feature.properties?.cluster_id)
const coordinates = feature.geometry.coordinates as [number, number]
const source = map.getSource('places') as mapboxgl.GeoJSONSource
source.getClusterExpansionZoom(clusterId, (error, zoom) => {
if (error || zoom === null || zoom === undefined) return
map.easeTo({
center: coordinates,
zoom,
})
})
})
getClusterExpansionZoom 返回该聚合开始拆分的缩放级别,比固定增加两级更适合不同密度的数据。
下面的组件实现了同一套逻辑。点击蓝色聚合圆点后,组件会显示该聚合包含的点数和目标 zoom,地图随后放大;继续点击新的聚合点,可以看到它逐步拆分为更小的聚合或绿色单点:
⚖️ Cluster 的适用边界
聚合适合大量独立点和“先看分布,再逐步放大”的任务,例如门店、设备和事件位置。需要同时比较每个点、点之间不能合并,或数据本身不是点时,不应强行聚合,可考虑按缩放级别过滤、服务端切片或其他表达方式。
Cluster 由 Mapbox 在 Source 中计算并用 WebGL Layer 绘制,通常比大量 DOM Marker 更适合密集点。但数据量、设备性能和样式复杂度仍会影响结果,应使用真实数据测试。
🧮 在聚合点上汇总业务字段
默认聚合结果只有 point_count。如果点对象还带有 value、online 等字段,可以用 clusterProperties 计算总数或分类数量:
map.addSource('places', {
type: 'geojson',
data: placeGeoJSON,
cluster: true,
clusterRadius: 50,
clusterProperties: {
totalValue: ['+', ['get', 'value']],
onlineCount: ['+', ['case', ['get', 'online'], 1, 0]],
},
})
totalValue 把聚合范围内每个点的 value 相加,onlineCount 把 online 为 true 的点记为 1 后再求和。生成的字段可以像 point_count 一样用于 Symbol 文字或颜色表达式。
clusterProperties 只聚合点 Source 中已有的属性。涉及去重、跨表统计或精确报表时,应由后端计算,地图只显示结果。
🧾 小节总结
- 聚合把屏幕上相近的点合成一个显示对象。
clusterRadius控制像素聚合范围,clusterMaxZoom控制停止聚合的层级。- 本文示例用三个 Layer 分别显示聚合圆、数量文字和未聚合点。
step可以按数量改变圆点颜色和大小。- 点击时用
getClusterExpansionZoom定位到合理层级。 clusterProperties可以汇总聚合范围内的数值和分类数量。- 聚合适合查看整体分布,不适合必须同时比较全部单点的任务。
❓ 知识问答
Q1:聚合后的 point_count 是原始字段吗?
不是,它是聚合 Source 为聚合 Feature 自动生成的属性。
Q2:为什么放大后仍然聚合?
当前缩放级别可能还没有超过 clusterMaxZoom,或点之间仍在 clusterRadius 范围内。
Q3:Cluster 能用于 Polygon 吗?
不能,GeoJSON Source 的内置聚合面向 Point Feature。
🧪 小练习
- 调整
clusterRadius,比较聚合数量和分布变化。 - 为聚合点实现点击放大,为单点实现点击 Popup。
// 请在这里编写代码
🎉 恭喜你已经掌握点数据聚合展示啦!
