代码
import * as mapvthree from '@baidumap/mapv-three';

mapvthree.BaiduMapConfig.ak = '您的密钥';
const {BaiduVectorTileProvider, Engine, GeoJSONDataSource, Polyline} = mapvthree;

const container = document.getElementById('map');
const center = [116.404495, 39.913867];
const aspect = container.clientWidth / container.clientHeight;
const range = (2_410 * 1.35) / Math.min(1.35, aspect);
const engine = new Engine(container, {
  map: {
    projection: 'EPSG:3857',
    center,
    pitch: 0,
    heading: 0,
    range,
    provider: new BaiduVectorTileProvider({
      displayOptions: {
        building: false,
        flat: true,
        poi: true,
      },
    }),
  },
  rendering: {
    sky: null,
    enableAnimationLoop: false,
  },
});

const source = createRouteSource(routeCoordinates());
let trail;
function showLine(lineWidth) {
  // 线宽改变时重新生成平面线的几何,继续复用同一份路线。
  if (trail) engine.remove(trail);
  trail = engine.add(
    new Polyline({
      flat: true,
      keepSize: true,
      color: '#2c6cd6',
      lineWidth,
    }),
  );
  trail.dataSource = source;
  engine.requestRender();
}
showLine(8);

const width = document.getElementById('width');
const value = document.getElementById('width-value');
width.addEventListener('click', () => {
  const wide = trail.lineWidth === 8;
  showLine(wide ? 16 : 8);
  width.setAttribute('aria-pressed', String(wide));
  width.textContent = wide ? '切换至 8 像素' : '切换至 16 像素';
  value.textContent = trail.lineWidth + ' 像素';
  engine.requestRender();
});

const zoom = document.getElementById('zoom');
zoom.addEventListener('click', () => {
  const close = zoom.getAttribute('aria-pressed') === 'false';
  engine.map.lookAt(center, {
    range: close ? range * 0.63 : range,
    heading: 0,
    pitch: 0,
  });
  zoom.setAttribute('aria-pressed', String(close));
  zoom.textContent = close ? '恢复全览' : '拉近观察';
});

function createRouteSource(coordinates) {
  // 给展示线的 BD-09 顶点附加少量高度,避开与矢量路面的共面深度争用。
  const positions = coordinates.map(([lng, lat]) => [lng, lat, 5]);
  return GeoJSONDataSource.fromGeoJSON({
    type: 'Feature',
    geometry: {
      type: 'LineString',
      coordinates: positions,
    },
    properties: {},
  });
}

// 北京长安街方向规划的驾车路段,宽度仅为显示样式,坐标为 BD-09 经纬度。
function routeCoordinates() {
  return [
    [116.39608, 39.913557],
    [116.404893, 39.913856],
    [116.406473, 39.913947],
    [116.409394, 39.914042],
    [116.410185, 39.91412],
    [116.41291, 39.914176],
  ];
}

在天安门前的长安街绘制平面宽线。切换 8 或 16 像素线宽,再缩放地图观察 keepSize 如何保持屏幕宽度。

预览