代码
import * as mapvthree from '@baidumap/mapv-three';
mapvthree.BaiduMapConfig.ak = '您的密钥';
const {BaiduVectorTileProvider, Engine, GeoJSONDataSource, Polyline} = mapvthree;
const container = document.getElementById('map');
const center = [116.399048, 39.929197];
const aspect = container.clientWidth / container.clientHeight;
const range = (1030 * 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: false,
},
}),
},
rendering: {
sky: null,
enableAnimationLoop: false,
},
});
// 平面模式支持原生虚线实段占比。
const trail = engine.add(
new Polyline({
flat: true,
color: '#ba4a13',
keepSize: true,
lineWidth: 7,
dashed: true,
alphaTest: 0.5,
dashArray: 32,
dashRatio: 0.48,
}),
);
trail.dataSource = createRouteSource(routeCoordinates());
const ratio = document.getElementById('ratio');
const value = document.getElementById('ratio-value');
ratio.addEventListener('input', () => {
trail.dashRatio = Number(ratio.value) / 100;
value.textContent = ratio.value + '%';
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: {},
});
}
// 北海公园南门至景山公园南门步行路线中的文津街、景山前街路段,坐标为 BD-09 经纬度。
function routeCoordinates() {
return [
[116.39603202789, 39.928805747246],
[116.39697704529, 39.928804364007],
[116.39716119792, 39.928755259009],
[116.39738128277, 39.928897040951],
[116.39761663881, 39.929161169546],
[116.39781875755, 39.929316160474],
[116.39804692715, 39.92943304318],
[116.39821221537, 39.9294717735],
[116.40206325109, 39.929639143556],
];
}
以文津街和景山前街的真实步行路段展示平面 Polyline 虚线。示例中心当前版本的屏幕线使用固定虚线节奏,本例用 flat 模式的原生 dashRatio 调节实段占比。
预览