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

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

const container = document.getElementById('map');
const center = [120.400582, 36.076879];
const aspect = container.clientWidth / container.clientHeight;
const range = (1_560 * 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: true,
  },
});

const arrow = createArrowTexture();
const trail = engine.add(
  new Polyline({
    flat: true,
    keepSize: true,
    lineWidth: 15,
    color: '#6e44bb',
    mapSrc: arrow,
    mapGap: 0.7,
    mapBlend: true,
    transparent: true,
  }),
);
trail.dataSource = createRouteSource(routeCoordinates());

const toggle = document.getElementById('texture');
toggle.addEventListener('click', () => {
  const enabled = toggle.getAttribute('aria-pressed') === 'false';
  trail.mapSrc = enabled ? arrow : '';
  toggle.setAttribute('aria-pressed', String(enabled));
  engine.requestRender();
});

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: {},
  });
}

function createArrowTexture() {
  const canvas = document.createElement('canvas');
  canvas.width = 64;
  canvas.height = 64;
  const context = canvas.getContext('2d');
  context.strokeStyle = '#fffbed';
  context.lineWidth = 7;
  context.lineCap = 'round';
  context.lineJoin = 'round';
  context.beginPath();
  context.moveTo(22, 15);
  context.lineTo(44, 32);
  context.lineTo(22, 49);
  context.stroke();
  return canvas.toDataURL();
}

// 青岛闽江路至闽江二路的百度驾车路线短段,坐标为 BD-09 经纬度。
function routeCoordinates() {
  return [
    [120.395786, 36.07676],
    [120.396479, 36.077037],
    [120.397233, 36.077433],
    [120.397545, 36.077557],
    [120.397937, 36.077669],
    [120.39852, 36.077709],
    [120.40018, 36.077728],
    [120.401215, 36.077709],
    [120.402442, 36.077517],
    [120.403568, 36.077267],
    [120.40393, 36.07724],
    [120.404372, 36.077173],
    [120.404674, 36.077097],
    [120.405378, 36.076976],
    [120.405137, 36.07603],
  ];
}

把 Canvas 绘制的箭头纹理沿青岛闽江路至闽江二路的驾车路段铺贴。缩放时线宽保持固定,可切换纹理和纯色路径。

预览