代码
import {
BaiduMapConfig,
BaiduVectorTileProvider,
DrawerType,
Editor,
Engine,
} from '@baidumap/mapv-three';
import {BoxGeometry, Mesh, MeshBasicMaterial} from 'three';
const container = document.querySelector('#map');
const info = document.querySelector('.info');
const operate = document.querySelector('.operate');
const center = [116.41663259609102, 39.92270391129512];
// 示例中心会注入 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: container.clientWidth < 500 ? 510 : 235,
pitch: 24,
heading: 0,
},
clock: {
currentTime: new Date('2026-06-21T02:30:00Z'),
},
rendering: {
enableAnimationLoop: false,
},
});
const editor = engine.add(
new Editor({
type: DrawerType.POLYGON,
renderOptions: {
depthTest: false,
renderOrder: 3,
},
}),
);
editor.setStyle({
fillColor: '#288f7a',
fillOpacity: 0.6,
strokeColor: '#116b61',
strokeWidth: 3,
strokeOpacity: 1,
});
const shape = operate.querySelector('#shape');
function startDrawing() {
editor.stop();
editor.type = shape.value;
editor.start();
info.textContent = '';
}
operate.querySelector('#draw').onclick = startDrawing;
operate.querySelector('#stop').onclick = () => {
editor.stop();
const count = editor.exportData().features.length;
info.textContent = count ? `已绘制 ${count} 个图形` : '';
};
operate.querySelector('#clear').onclick = () => {
editor.stop();
editor.delete();
info.textContent = '';
};
shape.onchange = startDrawing;
editor.addEventListener('created', () => {
info.textContent = `已绘制 ${editor.exportData().features.length} 个图形`;
});
startDrawing();
function addBlock(x, y, z, width, depth, height, color, angle = 0) {
const block = new Mesh(
new BoxGeometry(width, depth, height),
new MeshBasicMaterial({
color,
}),
);
block.position.set(x, y, z);
block.rotation.z = angle;
stage.add(block);
}
选择折线、多边形、点、圆或矩形,在演示地块上绘制。完成后可继续创建新图形,并在信息栏查看已绘制数量。 折线和多边形逐点绘制,按「完成绘制」保存。 注入有效 AK 后可在百度矢量底图上操作。
预览