代码
import * as mapvthree from '@baidumap/mapv-three';
mapvthree.BaiduMapConfig.ak = '您的密钥';
const engine = new mapvthree.Engine(document.getElementById('map'), {
map: {
provider: new mapvthree.BaiduVectorTileProvider(),
center: [120.145, 30.248],
range: 3600,
pitch: 0,
},
widgets: {
geoLocate: {
enabled: true,
// 1 表示 WGS84,与示例地图的输入坐标一致。
coordsType: 1,
},
},
});
const status = document.getElementById('location-status');
if (!navigator.geolocation) {
status.textContent = '当前页面无法使用浏览器定位';
}
const marker = engine.add(
new mapvthree.SimplePoint({
color: '#1164aa',
size: 20,
depthTest: false,
}),
);
const locationData = new mapvthree.DataSource();
marker.dataSource = locationData;
engine.widgets.geoLocate.addSuccessListener(({longitude, latitude}) => {
locationData.clear();
locationData.add(new mapvthree.DataItem([longitude, latitude, 5]));
engine.requestRender();
status.textContent = '已移动到浏览器提供的位置';
});
engine.widgets.geoLocate.addFailListener(({code}) => {
status.textContent = code === 1 ? '浏览器未授权定位' : '当前无法获取位置';
});
点击左下角的定位按钮并授权,地图会移动到浏览器提供的位置。请在 HTTPS 或 localhost 打开,嵌入页面需允许地理定位。示例使用浏览器的 WGS84 坐标。
预览