代码
import {
BaiduMapConfig,
BaiduVectorTileProvider,
Engine,
GeoJSONDataSource,
Spark,
} from '@baidumap/mapv-three';
const container = document.getElementById('map');
const center = [116.3985, 40.033];
const range = (1500 * 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: 62,
heading: 0,
provider,
},
rendering: {
sky: null,
enableAnimationLoop: true,
},
});
function point(coordinates, properties = {}) {
return {
type: 'Feature',
geometry: {
type: 'Point',
coordinates,
},
properties,
};
}
// 高饱和颜色在浅色底图上更醒目。
const colors = ['#ff3b30', '#ff9500', '#007aff'];
const hotspots = [
[116.395, 40.0345],
[116.3985, 40.0318],
[116.4022, 40.034],
];
// 每个热点周围按固定角度序列排布 40 个点,保证每次打开效果一致。
const points = hotspots.flatMap(([lng, lat], group) =>
Array.from({length: 40}, (_, index) => {
const angle = index * 2.399963;
const radius = 0.0011 * Math.sqrt((index + 0.5) / 40);
return point([lng + Math.cos(angle) * radius, lat + Math.sin(angle) * radius * 0.8], {
color: colors[group],
height: 220 + (index % 5) * 60,
});
}),
);
const source = GeoJSONDataSource.fromGeoJSON(points);
source.defineAttribute('color').defineAttribute('height');
const spark = engine.add(
new Spark({
speed: 0.2,
idle: 300,
// 拖尾越长,线条越明显。
tailLength: 140,
vertexColors: true,
vertexHeights: true,
}),
);
spark.dataSource = source;
在北京奥林匹克森林公园南门一带的三个热点绘制上升线条,颜色和高度按模拟数据逐点设置。
预览