代码
import {
BaiduMapConfig,
BaiduVectorTileProvider,
Engine,
GeoJSONDataSource,
Polygon,
} 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.143, 30.249],
range: 5100 / Math.min(1, aspect),
pitch: 0,
provider: new BaiduVectorTileProvider(),
},
});
// 两处示意观察区,坐标按 GeoJSON 的经度、纬度顺序编写。
const zones = [
{
name: '湖心观察区',
ring: [
[120.134, 30.247],
[120.144, 30.246],
[120.147, 30.252],
[120.137, 30.254],
],
},
{
name: '沿岸观察区',
ring: [
[120.15, 30.247],
[120.153, 30.247],
[120.153, 30.255],
[120.15, 30.253],
],
},
];
const collection = {
type: 'FeatureCollection',
features: zones.map(({name, ring}) => ({
type: 'Feature',
geometry: {type: 'Polygon', coordinates: [[...ring, ring[0]]]},
properties: {name},
})),
};
const areas = engine.add(
new Polygon({
color: '#2c9f9a',
opacity: 0.48,
transparent: true,
border: true,
borderColor: '#10656f',
borderWidth: 2,
}),
);
areas.dataSource = GeoJSONDataSource.fromGeoJSON(collection);
将西湖局部两处示意区域写成 GeoJSON FeatureCollection,加载为带描边的面图层。
预览