原文链接及内容

运行界面

这个示例展示在declutter属性被设置为true时,矢量(切片)图层如何被整理(清理/精简,这里不确定如何翻译)。默认情况下,在所有图层被渲染完毕后才会发生整理操作,调用地图(即Map对象)的flushDeclutter()方法可以使整理操作立即执行。这对于需要完全渲染在层堆栈中处于较低图层的整理项之上的图层很有用。在这个例子中,蓝色正方形覆盖显示在整理后的矢量符号和标签上方。

main.js代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import {Feature, Map, View} from 'ol/index.js';
import {Group as LayerGroup, Vector as VectorLayer} from 'ol/layer.js';
import {Vector as VectorSource} from 'ol/source.js';
import {apply} from 'ol-mapbox-style';
import {fromExtent} from 'ol/geom/Polygon.js';
import {getCenter} from 'ol/extent.js';

const square = [-12e6, 3.5e6, -10e6, 5.5e6];
const overlay = new VectorLayer({
source: new VectorSource({
features: [new Feature(fromExtent(square))],
}),
style: {
'stroke-color': 'rgba(180, 180, 255, 1)',
'stroke-width': 1,
'fill-color': 'rgba(200, 200, 255, 0.85)',
},
});

const layer = new LayerGroup();
apply(
layer,
'https://api.maptiler.com/maps/topo-v2/style.json?key=Get your own API key at https://www.maptiler.com/cloud/'
);

const map = new Map({
target: 'map',
view: new View({
center: getCenter(square),
zoom: 4,
}),
layers: [layer, overlay],
});

overlay.on('prerender', () => map.flushDeclutterItems());

界面布局文件index.html代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Declutter Group</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
.map .ol-rotate {
left: .5em;
bottom: .5em;
top: auto;
right: auto;
}
.map:-webkit-full-screen {
height: 100%;
margin: 0;
}
.map:fullscreen {
height: 100%;
}
</style>
</head>
<body>
<div id="map" class="map"></div>

<script type="module" src="main.js"></script>
</body>
</html>