原文链接及内容

运行界面

演示ol/format/WMSGetFeatureInfo格式对象中layers选项的用法,该选项允许单个WMS GetFeatureInfo请求按图层名称读取多个图层并返回要素信息。

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
import WMSGetFeatureInfo from 'ol/format/WMSGetFeatureInfo.js';

fetch('data/wmsgetfeatureinfo/osm-restaurant-hotel.xml')
.then(function (response) {
return response.text();
})
.then(function (response) {
// this is the standard way to read the features
const allFeatures = new WMSGetFeatureInfo().readFeatures(response);
document.getElementById('all').innerText = allFeatures.length.toString();

// when specifying the 'layers' options, only the features of those
// layers are returned by the format
const hotelFeatures = new WMSGetFeatureInfo({
layers: ['hotel'],
}).readFeatures(response);
document.getElementById('hotel').innerText =
hotelFeatures.length.toString();

const restaurantFeatures = new WMSGetFeatureInfo({
layers: ['restaurant'],
}).readFeatures(response);
document.getElementById('restaurant').innerText =
restaurantFeatures.length.toString();
});

界面布局文件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
33
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WMS GetFeatureInfo (Layers)</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
</style>
</head>
<body>
<table id="info">
<tr>
<td>All features:</td>
<td id="all"></td>
</tr>
<tr>
<td>Hotel features:</td>
<td id="hotel"></td>
</tr>
<tr>
<td>Restaurant features:</td>
<td id="restaurant"></td>
</tr>
</table>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="https://cdn.jsdelivr.net/npm/elm-pep@1.0.6/dist/elm-pep.js"></script>
<script type="module" src="main.js"></script>
</body>
</html>