代码
import {BaiduMapConfig, BaiduVectorTileProvider, Engine} from '@baidumap/mapv-three';
import {Mesh, MeshBasicMaterial, SphereGeometry} from 'three';
const container = document.getElementById('map');
const center = [116.404, 39.915];
const range = (500 * 1.42) / Math.min(1.42, container.clientWidth / container.clientHeight);
BaiduMapConfig.ak = '您的密钥';
const provider = new BaiduVectorTileProvider({
displayOptions: {
building: false,
poi: false,
},
});
const engine = new Engine(container, {
map: {
projection: 'EPSG:3857',
center,
range,
pitch: 45,
heading: 0,
provider,
},
rendering: {
sky: null,
},
});
// MeshBasicMaterial 不受光照影响,没有天空时也能正常显示颜色。
const mesh = engine.add(
new Mesh(new SphereGeometry(30, 32, 32), new MeshBasicMaterial({color: 0x3aa1ff})),
);
mesh.position.fromArray(engine.map.projectArrayCoordinate([116.404, 39.915, 30]));
// 创建后直接挂上变换控制器,拖动轴即可平移、旋转、缩放。
engine.selection.attachTransform(mesh);
// 点击球体切换选中状态,选中时显示包围盒。
mesh.addEventListener('click', () => {
if (engine.selection.isSelected(mesh)) {
engine.selection.deselect(mesh);
} else {
engine.selection.select(mesh);
}
});
在天安门上空放一个球体,创建后直接挂上变换控制器,拖轴平移、旋转、缩放;点击球体切换选中并显示包围盒。
预览