代码
import {
  BaiduMapConfig,
  BaiduVectorTileProvider,
  Engine,
  GeoJSONDataSource,
  MockTwin,
  Twin,
  Polyline,
  twinConstants,
} from '@baidumap/mapv-three';

const container = document.querySelector('#map');
const info = document.querySelector('.info');
const operate = document.querySelector('.operate');
const center = [116.40291182145938, 39.93219301296199];
// 示例中心会注入 AK,复制代码时可填写自己的密钥。
const ak = BaiduMapConfig.ak || '您的密钥';
const hasKey = Boolean(ak.trim()) && !ak.includes('密钥');
const provider = hasKey
  ? new BaiduVectorTileProvider({
      ak,
      displayOptions: {
        flat: true,
        building: false,
      },
    })
  : null;
const engine = new Engine(container, {
  map: {
    projection: 'ecef',
    provider,
    center,
    range: (1140 * 1.14) / Math.min(1.14, container.clientWidth / container.clientHeight),
    pitch: 41,
    heading: 0,
  },
  clock: {
    currentTime: new Date('2026-06-21T02:30:00Z'),
  },
  rendering: {
    enableAnimationLoop: true,
  },
});
// 景山公园四周的道路中心线,BD-09 经纬度。
const route = [
  [116.40015134574526, 39.92951160949869],
  [116.40598864622852, 39.929742549328346],
  [116.40578391075242, 39.93450881154934],
  [116.40575840566817, 39.93462353952018],
  [116.40567540642792, 39.93472412524997],
  [116.4055403440982, 39.9347780080741],
  [116.40531986913238, 39.93480222421779],
  [116.40041471323646, 39.93473490926688],
  [116.40018080007968, 39.93470454480644],
  [116.40000781997928, 39.934602613473345],
  [116.39994313508183, 39.93445167534396],
  [116.40015134574526, 39.92951160949869],
];

// 离线预览显示车辆的实际路线。百度底图接通时直接使用底图道路。
if (!provider) {
  const line = engine.add(
    new Polyline({
      color: '#387f87',
      width: 4,
      vertexHeights: true,
    }),
  );
  line.dataSource = GeoJSONDataSource.fromGeoJSON([
    {
      type: 'Feature',
      geometry: {
        type: 'LineString',
        coordinates: route.map(([lng, lat]) => [lng, lat, 0.1]),
      },
    },
  ]);
}

// 车型编号 3 对应轿车,6 对应公交车,均使用随 SDK 提供的写实模型。
const models = twinConstants.REALISTIC_TEMPLATE_MODEL;
const modelConfig = {
  3: models.CAR,
  6: models.BUS,
};
const simulation = engine.add(
  new MockTwin(
    {
      delay: 700,
      modelConfig,
      keepSize: true,
      maxScale: 6,
    },
    route.map(([lng, lat]) => [lng, lat, 0.45]),
    {
      color: {
        '#438bed': 0.6,
        '#e5a03a': 0.4,
      },
      modelType: {
        3: 0.55,
        6: 0.45,
      },
    },
  ),
);
// 设置 MockTwin 所挂载 Twin 的显示比例,便于辨认车身。
const traffic = simulation.children.find((child) => child instanceof Twin);
// 将 MockTwin 的局部北向角换算为 Twin 从正东逆时针计的弧度。
traffic.dataProvider.process('dir', ({hpr}) => ((hpr.heading + 90) * Math.PI) / 180);
traffic.dataProvider.process('scale', ({modelType}) => {
  const size = Number(modelType) === 6 ? 2.5 : 3.1;
  return [size, size, size];
});
const speed = operate.querySelector('#speed');
const play = operate.querySelector('#traffic-play');
let running = true;
function startSimulation() {
  simulation.start({
    speed: speed.valueAsNumber,
    gap: 180,
    initialCount: 10,
  });
}
speed.oninput = () => {
  simulation.speed = speed.valueAsNumber;
  info.textContent = `模拟车流 · ${speed.value} km/h`;
};
play.onclick = () => {
  running = !running;
  if (running) {
    startSimulation();
  } else {
    simulation.clear();
  }
  speed.disabled = !running;
  info.textContent = running ? `模拟车流 · ${speed.value} km/h` : '模拟已停止';
  play.textContent = running ? '停止车流' : '启动车流';
};
startSimulation();
speed.oninput();

window.addEventListener('pagehide', ({persisted}) => {
  if (!persisted) {
    simulation.clear();
    engine.dispose();
  }
});

用 MockTwin 在北京景山公园四周的实际道路上模拟写实轿车与公交车。可调节车速、停止和重启。

预览