代码
import {
BaiduMapConfig,
BaiduVectorTileProvider,
DynamicSky,
Engine,
GeoJSONDataSource,
Heatmap3D,
} from '@baidumap/mapv-three';
const container = document.getElementById('map');
const center = [116.39815, 40.03305];
const range = (3_050 * 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: 51,
heading: 18,
provider,
},
rendering: {
sky: null,
enableAnimationLoop: false,
},
});
engine.add(new DynamicSky());
function point(coordinates, properties = {}) {
return {
type: 'Feature',
geometry: {
type: 'Point',
coordinates,
},
properties,
};
}
function sampleCloud([x, y, count, spread, weight]) {
return Array.from({length: count}, (_, index) => {
// 固定角度与半径序列让同一批采样每次打开都保持一致。
const angle = index * 2.399963;
const radius = Math.sqrt((index + 0.5) / count) * spread;
return point([x + Math.cos(angle) * radius, y + Math.sin(angle) * radius * 0.8], {
count: weight * (1 - (radius / spread) * 0.35),
});
});
}
const hotspots = [
[116.39155, 40.03452, 65, 0.00172, 2.2],
[116.3965, 40.03825, 36, 0.00148, 1.4],
[116.39825, 40.03248, 76, 0.00189, 2.9],
[116.4052, 40.0341, 48, 0.0018, 2.0],
[116.3969, 40.02798, 43, 0.00152, 1.7],
[116.4029, 40.02899, 27, 0.00115, 1.0],
];
const source = GeoJSONDataSource.fromGeoJSON(hotspots.flatMap(sampleCloud));
source.defineAttribute('count');
const heat = engine.add(
new Heatmap3D({
radius: 150,
maxValue: 60,
heightRatio: 230,
gradient: {
0.15: '#304f99',
0.38: '#237baf',
0.67: '#eda83a',
1: '#df452e',
},
}),
);
heat.dataSource = source;
const height = document.getElementById('height');
height.addEventListener('input', () => {
heat.heightRatio = Number(height.value);
document.getElementById('height-value').textContent = height.value;
engine.requestRender();
});
将奥林匹克森林公园不同片区的模拟客流渲染成疏密不同的热力峰。斜俯视比较各热区分布,拖动滑块观察峰高变化。
预览