WMS GetFeatureInfo (图层)
WMS GetFeatureInfo (图层)示例。
演示 ol/format/WMSGetFeatureInfo 格式对象中 layers 选项的用法,该选项允许在单个 WMS GetFeatureInfo 请求查询多个图层时,按图层名称读取返回的要素。
main.js
js
import WMSGetFeatureInfo from 'ol/format/WMSGetFeatureInfo.js';
fetch('data/wmsgetfeatureinfo/osm-restaurant-hotel.xml')
.then(function (response) {
return response.text();
})
.then(function (response) {
// 这是读取要素的标准方式
const allFeatures = new WMSGetFeatureInfo().readFeatures(response);
document.getElementById('all').innerText = allFeatures.length.toString();
// 当指定 'layers' 选项时,格式对象只返回
// 这些图层的要素
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
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WMS GetFeatureInfo(多图层)</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
</style>
</head>
<body>
<table id="info">
<tr>
<td>所有要素:</td>
<td id="all"></td>
</tr>
<tr>
<td>酒店要素:</td>
<td id="hotel"></td>
</tr>
<tr>
<td>餐厅要素:</td>
<td id="restaurant"></td>
</tr>
</table>
<script type="module" src="main.js"></script>
</body>
</html>package.json
json
{
"name": "getfeatureinfo-layers",
"dependencies": {
"ol": "10.11.0"
},
"devDependencies": {
"vite": "^8.1.5"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}