代码
import {BaiduMapConfig, BaiduVectorTileProvider, Editor, Engine} from '@baidumap/mapv-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 data = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
id: 'plot-01',
properties: {
name: '公共活动场地',
},
geometry: {
type: 'Polygon',
coordinates: [
[
toGeo(-58, -38, 0.4),
toGeo(52, -38, 0.4),
toGeo(58, 38, 0.4),
toGeo(-31, 55, 0.4),
toGeo(-58, -38, 0.4),
],
],
},
},
],
};
const editor = engine.add(
new Editor({
enableMidpointHandles: true,
editAxes: {
z: false,
},
}),
);
editor.setStyle({
fillColor: '#288f7a',
fillOpacity: 0.42,
strokeColor: '#116b61',
strokeWidth: 3,
strokeOpacity: 1,
});
const toggle = operate.querySelector('#toggle-edit');
function reset() {
editor.disableEdit();
editor.importData(structuredClone(data), {
clear: true,
});
editor.enableEdit('plot-01');
toggle.textContent = '结束编辑';
info.textContent = '';
}
toggle.onclick = () => {
if (editor.isEditing) {
editor.disableEdit();
toggle.textContent = '开始编辑';
return;
}
editor.enableEdit('plot-01');
toggle.textContent = '结束编辑';
};
operate.querySelector('#restore').onclick = reset;
editor.addEventListener('update', () => {
const ring = editor.exportData().features[0].geometry.coordinates[0];
info.textContent = `地块已更新 · ${ring.length - 1} 个顶点`;
});
reset();
// 小范围示例用局部米坐标布置景物,再转换为经纬度及高度。
function toGeo(x, y, height = 0) {
return [
center[0] + x / (111320 * Math.cos((center[1] * Math.PI) / 180)),
center[1] + y / 111050,
height,
];
}
将预设 GeoJSON 导入 Editor 后,点击顶点后拖动控制手柄改变形状,也可点击边中点插入新顶点。恢复按钮重新载入初始边界。 注入有效 AK 后可在百度矢量底图上操作。
预览