代码
import {
BaiduMapConfig,
BaiduVectorTileProvider,
Engine,
GeoJSONDataSource,
Polygon,
} from '@baidumap/mapv-three';
const container = document.getElementById('map');
const center = [116.45394, 39.9366];
const range = (825 * 1.42) / Math.min(1.42, container.clientWidth / container.clientHeight);
BaiduMapConfig.ak = '您的密钥';
const provider = new BaiduVectorTileProvider({
displayOptions: {
building: true,
poi: false,
},
});
const engine = new Engine(container, {
map: {
projection: 'EPSG:3857',
center,
range,
pitch: 0,
heading: 0,
provider,
},
rendering: {
sky: null,
enableAnimationLoop: false,
},
});
// 取样自百度矢量底图中可见的工人体育场看台屋顶和内场边缘。
const stands = [
[116.453745, 39.937795],
[116.453502, 39.937745],
[116.453283, 39.937652],
[116.453072, 39.937502],
[116.45295, 39.937372],
[116.452779, 39.937104],
[116.452706, 39.93688],
[116.452682, 39.936581],
[116.452706, 39.936376],
[116.452779, 39.936146],
[116.452909, 39.935922],
[116.453072, 39.935741],
[116.453274, 39.935592],
[116.45351, 39.935486],
[116.453729, 39.935436],
[116.453997, 39.93543],
[116.45424, 39.93548],
[116.454468, 39.935579],
[116.45467, 39.935723],
[116.454833, 39.935903],
[116.454954, 39.936108],
[116.455036, 39.936351],
[116.45506, 39.936575],
[116.455044, 39.936787],
[116.454954, 39.937092],
[116.454833, 39.937303],
[116.45467, 39.937484],
[116.454468, 39.937633],
[116.45424, 39.937739],
[116.454013, 39.937789],
];
const infield = [
[116.45381, 39.937297],
[116.453948, 39.937297],
[116.454094, 39.937266],
[116.454216, 39.93721],
[116.454354, 39.93711],
[116.454508, 39.936899],
[116.454557, 39.936756],
[116.454573, 39.936625],
[116.454557, 39.936463],
[116.454516, 39.936332],
[116.454386, 39.936146],
[116.454192, 39.936003],
[116.454086, 39.935959],
[116.453964, 39.935934],
[116.453818, 39.935934],
[116.453672, 39.935965],
[116.45355, 39.936021],
[116.453421, 39.936115],
[116.453266, 39.93632],
[116.453201, 39.936532],
[116.45321, 39.936774],
[116.453258, 39.936911],
[116.453331, 39.937029],
[116.453526, 39.937204],
[116.45368, 39.937272],
];
function closeRing(boundary) {
return [...boundary, boundary[0]];
}
function footprint(keepInfield) {
const rings = [closeRing(stands)];
if (keepInfield) {
rings.push(closeRing(infield));
}
return {
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: rings,
},
properties: {},
};
}
const stadium = engine.add(
new Polygon({
color: '#2765a5',
transparent: true,
opacity: 0.79,
border: true,
borderColor: '#233e58',
borderWidth: 2,
// 当前底图屋顶约高 22.7 米。多边形略高于屋顶以保持深度测试稳定。
zOffset: 24,
}),
);
const source = GeoJSONDataSource.fromGeoJSON([footprint(true)]);
stadium.dataSource = source;
const button = document.getElementById('hole');
let keepInfield = true;
button.addEventListener('click', () => {
keepInfield = !keepInfield;
// 外环保留,内环与外环绕向相反。
source.setData([footprint(keepInfield)]);
button.setAttribute('aria-pressed', String(keepInfield));
engine.requestRender();
});
沿北京工人体育场的看台屋顶绘制带洞多边形,用 GeoJSON 内环保留中间球场,并用 Polygon 原生描边勾勒外圈和内圈。切换为完全填充时,仅保留外圈描边。
预览