代码
import {
BaiduMapConfig,
BaiduVectorTileProvider,
Engine,
GeoJSONDataSource,
SimplePoint,
} from '@baidumap/mapv-three';
BaiduMapConfig.ak = '您的密钥';
const container = document.getElementById('map');
const aspect = container.clientWidth / container.clientHeight;
const engine = new Engine(container, {
map: {
center: [120.146, 30.25],
range: 5100 / Math.min(1, aspect),
pitch: 0,
provider: new BaiduVectorTileProvider({
displayOptions: {
poi: false,
},
}),
},
});
// 西湖沿岸的示意码头,count 为模拟的日客流量(百人次)。
const docks = [
{coordinates: [120.1405, 30.2585], count: 32, type: 'main'},
{coordinates: [120.1567, 30.2563], count: 18, type: 'branch'},
{coordinates: [120.1573, 30.2459], count: 24, type: 'main'},
{coordinates: [120.148, 30.2378], count: 12, type: 'branch'},
{coordinates: [120.1352, 30.2468], count: 15, type: 'branch'},
];
const dataSource = GeoJSONDataSource.fromGeoJSON(
docks.map(({coordinates, count, type}) => ({
type: 'Feature',
geometry: {type: 'Point', coordinates},
properties: {count, type},
})),
);
// size 取自 count 字段;color 由回调按 type 计算。
dataSource
.defineAttribute('size', 'count')
.defineAttribute('color', (properties) => (properties.type === 'main' ? '#d9483b' : '#1267c7'));
// 开启 vertexSizes、vertexColors 后,图层才会读取数据中的 size、color。
const points = engine.add(
new SimplePoint({
vertexSizes: true,
vertexColors: true,
depthTest: false,
}),
);
points.dataSource = dataSource;
在西湖沿岸标出五个游船码头,点的大小按客流量、颜色按码头类型设置,演示 defineAttribute 与 vertexSizes、vertexColors 的配合。
预览