代码
import * as mapvthree from '@baidumap/mapv-three';
mapvthree.BaiduMapConfig.ak = '您的密钥';
const {
BaiduVectorTileProvider,
DynamicSky,
Engine,
GeoJSONDataSource,
Label,
Polyline,
SimplePoint,
} = mapvthree;
const container = document.getElementById('map');
const center = [116.401, 40.0066, 145];
const aspect = container.clientWidth / container.clientHeight;
const range = 3_950 / Math.min(1, aspect * 0.88);
const engine = new Engine(container, {
map: {
projection: 'EPSG:3857',
center,
pitch: 55,
heading: 34,
range,
provider: new BaiduVectorTileProvider({
displayOptions: {
building: true,
flat: false,
poi: false,
},
}),
},
rendering: {
sky: null,
enableAnimationLoop: true,
},
});
const sky = engine.add(new DynamicSky());
sky.time = 15 * 3600;
sky.cloudsCoverage = 0.45;
const sites = readLandmarks();
const origin = sites[0].coordinates;
const source = GeoJSONDataSource.fromGeoJSON(
sites.slice(1).map((site) => ({
type: 'Feature',
geometry: {
type: 'LineString',
// 原生曲线只需要端点。SDK 按地标间距离自动生成贝塞尔弧线。
coordinates: [origin, site.coordinates],
},
properties: {},
})),
);
const arcs = engine.add(
new Polyline({
isCurve: true,
color: '#5339b0',
keepSize: true,
lineWidth: 4,
}),
);
arcs.dataSource = source;
const flights = engine.add(
new Polyline({
isCurve: true,
color: '#ff972c',
keepSize: true,
lineWidth: 4,
enableAnimation: true,
animationTailType: 1,
animationTailRatio: 0.26,
animationSpeed: 0.15,
animationIdle: 0,
}),
);
flights.dataSource = source;
const anchors = GeoJSONDataSource.fromGeoJSON(
sites.map(({name, coordinates, textAnchor}) => ({
type: 'Feature',
geometry: {
type: 'Point',
coordinates,
},
properties: {
text: name,
textAnchor: aspect < 1 ? 'bottom' : textAnchor,
},
})),
);
anchors.defineAttribute('text').defineAttribute('textAnchor');
const dots = engine.add(
new SimplePoint({
color: '#41328c',
size: 10,
}),
);
dots.dataSource = anchors;
const labels = engine.add(
new Label({
type: 'text',
textSize: 14,
textFillStyle: '#281d59',
textStrokeStyle: '#ffffff',
textStrokeWidth: 3,
padding: [11, 11],
textOffset: [0, 20],
depthTest: false,
depthWrite: false,
}),
);
labels.dataSource = anchors;
const view = document.getElementById('view');
view.addEventListener('click', () => {
const top = view.getAttribute('aria-pressed') === 'false';
engine.map.lookAt(center, {
range: top ? range * 1.12 : range,
pitch: top ? 0 : 55,
heading: top ? 0 : 34,
});
view.setAttribute('aria-pressed', String(top));
view.textContent = top ? '恢复斜视' : '俯视';
});
function readLandmarks() {
// 地标位置来自百度地理编码,第三维高度用于让飞线端点露出建筑。
return [
{
name: '国家体育场',
textAnchor: 'left',
coordinates: [116.402152, 39.999924, 65],
},
{
name: '国家游泳中心',
textAnchor: 'right',
coordinates: [116.396981, 39.999217, 65],
},
{
name: '中国科技馆',
textAnchor: 'left',
coordinates: [116.40504, 40.012384, 65],
},
{
name: '奥林匹克塔',
textAnchor: 'right',
coordinates: [116.400632, 40.014687, 65],
},
];
}
以国家体育场为起点,向国家游泳中心、中国科技馆和奥林匹克塔绘制示意飞线,由 Polyline 的 isCurve 自动生成空间曲线。斜视显示真实三维底图与 DynamicSky,俯视查看端点的地理关系。
预览