代码
import {
  BaiduMapConfig,
  BaiduVectorTileProvider,
  ClusterPoint,
  Engine,
  GeoJSONDataSource,
  Icon,
  Label,
} from '@baidumap/mapv-three';

const container = document.getElementById('map');
const center = [116.409, 39.915];
const range = (9600 * 1.42) / Math.min(1.42, container.clientWidth / container.clientHeight);

BaiduMapConfig.ak = '您的密钥';
const provider = new BaiduVectorTileProvider({
  displayOptions: {
    building: false,
    poi: false,
  },
});

const engine = new Engine(container, {
  map: {
    projection: 'EPSG:3857',
    center,
    range,
    pitch: 0,
    heading: 0,
    provider,
  },
  rendering: {
    sky: null,
    enableAnimationLoop: true,
  },
});

function point(coordinates, properties = {}) {
  return {
    type: 'Feature',
    geometry: {
      type: 'Point',
      coordinates,
    },
    properties,
  };
}

function texture(width, height, draw) {
  const canvas = document.createElement('canvas');
  canvas.width = width;
  canvas.height = height;
  draw(canvas.getContext('2d'));
  return canvas.toDataURL();
}

const groups = [
  [116.385, 39.923, 37],
  [116.405, 39.915, 52],
  [116.4075, 39.9296, 45],
  [116.4294, 39.9256, 38],
  [116.424, 39.9061, 41],
  [116.4005, 39.901, 46],
  [116.442, 39.915, 39],
];
const locations = groups.flatMap(([lng, lat, count]) =>
  Array.from({length: count}, (_, index) => {
    const angle = index * 2.399963;
    const radius = 0.00127 * Math.sqrt((index + 0.5) / count);
    return point([lng + Math.cos(angle) * radius, lat + Math.sin(angle) * radius * 0.75, 5]);
  }),
);

const badge = texture(64, 64, (context) => {
  context.fillStyle = '#126b73';
  context.strokeStyle = '#84ebe3';
  context.lineWidth = 4;
  context.beginPath();
  context.arc(32, 32, 27, 0, Math.PI * 2);
  context.fill();
  context.stroke();
});
const dot = texture(64, 64, (context) => {
  context.fillStyle = '#87f0df';
  context.beginPath();
  context.arc(32, 32, 7, 0, Math.PI * 2);
  context.fill();
});

const cluster = engine.add(
  new ClusterPoint({
    cluster: {
      minZoom: 5,
      maxZoom: 17,
      radius: 48,
    },
  }),
);
cluster.addComponent(
  new Icon({
    vertexIcons: true,
    width: 40,
    height: 40,
    depthTest: false,
  }),
);
cluster.addComponent(
  new Label({
    type: 'text',
    textSize: 15,
    textFillStyle: '#ffffff',
    textWeight: '600',
    depthTest: false,
  }),
);

// 聚合要素携带 point_count,拆分后的原始点只绘制小圆点。
cluster.clusterDataSource
  .defineAttribute('icon', (properties) => (properties.cluster ? badge : dot))
  .defineAttribute('text', (properties) =>
    properties.cluster ? String(properties.point_count) : '',
  )
  .defineAttribute('count', (properties) => properties.point_count ?? 1);
cluster.dataSource = GeoJSONDataSource.fromGeoJSON(locations);

const count = document.getElementById('count');
const total = '共 ' + locations.length + ' 个模拟点位';
count.textContent = total;
cluster.addEventListener('click', ({entity}) => {
  if (entity?.pairs.count) {
    count.textContent = '所选位置 · ' + entity.pairs.count + ' 辆';
  }
});

const zoom = document.getElementById('zoom');
zoom.addEventListener('click', () => {
  const detail = zoom.getAttribute('aria-pressed') === 'false';
  const [lng, lat] = groups[1];
  engine.map.lookAt(detail ? [lng, lat] : center, {
    range: detail ? 520 / Math.min(1, container.clientWidth / container.clientHeight) : range,
    pitch: 0,
    heading: 0,
  });
  count.textContent = total;
  zoom.setAttribute('aria-pressed', String(detail));
  zoom.textContent = detail ? '返回全览' : '放大中心站点';
});

在北京中心城区分布 298 个模拟车辆点。缩放查看各片区聚合数量与拆分后的原始点,也可点选徽标查看该处车辆数。

预览