代码
import {BaiduMapConfig, BaiduVectorTileProvider, DOMOverlay, Engine} from '@baidumap/mapv-three';
const container = document.getElementById('map');
const center = [116.282246, 40.000074];
const range = (650 * 1.42) / Math.min(1.42, container.clientWidth / container.clientHeight);
BaiduMapConfig.ak = '您的密钥';
const provider = new BaiduVectorTileProvider({
displayOptions: {
building: false,
},
});
const engine = new Engine(container, {
map: {
projection: 'EPSG:3857',
center,
range,
pitch: 0,
heading: 0,
provider,
},
rendering: {
sky: null,
enableAnimationLoop: true,
},
});
const card = document.createElement('div');
card.className = 'observation-card';
const title = document.createElement('strong');
title.textContent = '水质观测点';
const hint = document.createElement('span');
hint.textContent = '拖动以移动锚点';
card.append(title, hint);
const origin = [116.281796, 40.000294, 0];
const overlay = engine.add(
new DOMOverlay({
point: origin,
dom: card,
enableDragging: true,
}),
);
const result = document.getElementById('coordinate');
function showCoordinates() {
const [lng, lat] = overlay.point;
result.textContent = lng.toFixed(5) + '°, ' + lat.toFixed(5) + '°';
}
overlay.addEventListener('dragend', showCoordinates);
document.getElementById('reset').addEventListener('click', () => {
overlay.point = origin;
showCoordinates();
engine.requestRender();
});
showCoordinates();
将一个 HTMLElement 传给 DOMOverlay 并开启拖动,显示拖动完成后的地理坐标及其复位方法。 使用北京颐和园昆明湖的百度矢量地图,覆盖物数据用于演示。
预览