Skip to content

IconClusterBasicLayer 基础图标聚合 ​

IconClusterBasicLayer 使用 Cesium 内置 EntityCluster 将相邻的屏幕点位聚合,相机缩放和移动时自动更新。支持单点图标、数字圆标,以及图标与统计文字组合。

图层使用 SDK 点数据格式,不创建 Web Worker。需要 Worker 屏幕网格聚合时,可使用 IconClusterLayer。两者的分组规则不同:本图层按图标屏幕包围框与 pixelRange 扩展范围判断相邻关系,聚合数量和位置可能不同。

组件案例 ​

构造函数 ​

js
new MapLayers.IconClusterBasicLayer(viewer, config)
参数类型描述
viewerCesium.ViewerCesium Viewer 实例
configobject图标和聚合配置

config 配置 ​

参数类型默认值描述
enabledbooleantrue是否启用聚合
pixelRangenumber36图标屏幕包围框的扩展距离,单位为像素;越大越容易聚合,0 时相交的图标仍可能聚合
minimumClusterSizenumber2形成聚合图标所需的最少点数
iconstring / Canvas / Image内置定位图标单个点位的默认图标
widthnumber34单个图标宽度
heightnumber42单个图标高度
colorstring#ffffff单个图标的叠加颜色
offset[number, number][0, 0]单个图标的像素偏移
disableDepthTestDistancenumberInfinity超过此距离后关闭深度测试
clusterStylesArray见下方根据聚合数量设置图标或数字圆标
clusterTextColorstring#ffffff聚合数量文字颜色
clusterStrokeColorstring#d9f8ffff聚合图标描边颜色
clusterStrokeWidthnumber2聚合图标描边宽度
clusterFontSizenumber15聚合数量的基础字号
clusterShowTextbooleantrue聚合文字的总开关,false 隐藏所有统计文字
clusterTextstring / function'{count}'统计文字模板,支持 {count} 或 (count, entities) => string
clusterTextPositionstring'top'图标模式的文字位置:top、bottom、left、right
clusterTextGapnumber6图标与文字的间距,单位为像素
clusterTextOffset[number, number][0, 0]图标模式文字的额外像素偏移,x 向右、y 向下
clusterTextOutlineColorstring'#071b28'图标模式文字的描边颜色
clusterTextOutlineWidthnumber2图标模式文字的描边宽度

clusterStyles 按 min 从小到大匹配。例如聚合数量为 75 时,会使用 min: 50 对应的样式。

js
clusterStyles: [
  { min: 2, size: 34, color: '#1c86d1dd' },
  { min: 50, size: 40, color: '#43b86add' },
  { min: 100, size: 46, color: '#f56c6cdd' },
  { min: 200, size: 52, color: '#e6a23cdd' },
]

为了兼容常见 Cesium 聚合案例,也可以使用 colorArr,其中的 num 等同于 clusterStyles.min。

图标与统计文字 ​

clusterStyles 中传入 icon 后,聚合结果显示该图标,并在图标上方显示统计文字。未传 icon 时继续使用数字圆标。

样式字段类型默认值作用
minnumber按档位设置使用该样式的最少聚合数量
iconstring / Canvas / Image未设置聚合后的图标
width / heightnumbersize聚合图标宽高,单位为像素
sizenumber34数字圆标直径,以及未设置宽高时的图标尺寸
colorstring'#1c86d1dd'数字圆标的填充色,不改变 icon 的颜色
showTextbooleantrue是否显示当前档位的统计文字;总开关关闭时均隐藏
textstring / functionclusterText当前档位的统计文字模板或格式化函数
textPositionstringclusterTextPosition图标模式文字位置:top、bottom、left、right
textGapnumberclusterTextGap图标与文字的间距
textOffset[number, number]clusterTextOffset图标模式文字的额外偏移
textColor / fontSizestring / number对应全局字段文字颜色和字号
textOutlineColor / textOutlineWidthstring / number对应全局字段图标模式文字描边颜色和宽度
js
clusterStyles: [
  {
    min: 2,
    icon: '/images/device-marker.png',
    width: 36,
    height: 46,
    showText: true,
    textPosition: 'top',
    textGap: 6,
    text: '设备 {count} 台',
    textColor: '#ffffff',
    fontSize: 14,
  },
  {
    min: 50,
    icon: '/images/device-cluster.png',
    width: 44,
    height: 54,
    textPosition: 'right',
    text: (count, entities) => '共 ' + count + ' 台设备',
  },
]

样式中的文字配置优先于全局默认值。{count} 会替换为当前聚合数量;函数的第二个参数是该组的 Cesium.Entity[]。数字圆标的文字保持居中,textPosition、间距、额外偏移和文字描边用于图标模式。

数据格式 ​

setData 接受 BMapViewer 点数据数组,也支持 GeoJSON FeatureCollection。单个点位的 properties.icon、width、height、color 和 offset 可以覆盖全局配置。

js
layer.setData([
  {
    geometry: {
      type: 'Point',
      coordinates: [125.834, 44.147, 0],
    },
    properties: {
      id: 'device-001',
      name: '一号监测设备',
      color: '#8eeeff',
    },
  },
])

方法 ​

setData(data) ​

清空旧数据并批量加载点位。返回创建成功的 Cesium.Entity[]。

readyPromise ​

数据源挂载到 Viewer 后完成,返回 Cesium.CustomDataSource。不代表图标图片已下载完毕;可在构造后直接调用 setData。

load(dataOrUrl) ​

异步加载 GeoJSON 对象、URL 或 Cesium.Resource。

js
await layer.load('/data/devices.geojson')

addLayer(options) ​

添加单个点位并返回对应的 Cesium.Entity。

getLayerById(id) / removeLayerById(id) ​

根据数据中的 properties.id 查询或移除点位。

setEnabled(enabled) ​

动态开启或关闭聚合。关闭后,视野内的点以单点图标显示。

updateConfig(config) ​

更新 pixelRange、minimumClusterSize、clusterStyles 和统计文字配置,并立即触发重新聚合。

js
// 调整未在样式中单独指定的位置与文字模板
layer.updateConfig({ clusterTextPosition: 'right', clusterText: '共 {count} 台' })

// 隐藏全部聚合文字,保留聚合图标
layer.updateConfig({ clusterShowText: false })

show() / hide() ​

显示或隐藏整个图层。

clearLayer() / destroy() ​

清空数据,或销毁图层并移除数据源、聚合样式监听及 Cesium 聚合资源。

完整示例 ​

js
import { MapLayers } from 'b-map-viewer'

const layer = new MapLayers.IconClusterBasicLayer(viewer, {
  icon: '/images/device-marker.png',
  width: 32,
  height: 40,
  pixelRange: 48,
  minimumClusterSize: 2,
  clusterStyles: [
    { min: 2, size: 34, color: '#1c86d1dd' },
    { min: 20, size: 42, color: '#43b86add' },
    { min: 80, size: 50, color: '#f56c6cdd' },
  ],
})

layer.setData(points)

// 页面卸载时
layer.destroy()

基于 Apache-2.0 许可发布