Vector Wfs Getfeature
Runnable OpenLayers example: Vector Wfs Getfeature. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:OpenLayers WFS GetFeature 请求
要点:WFS 格式器生成带过滤条件的 GetFeature XML,页面内按条件筛选响应
依赖:OpenLayers 10.3.1 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>WFS GetFeature 请求</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.css">
<style>
html,body{height:100%;margin:0}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
.bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
display:flex;gap:10px;align-items:center;flex-wrap:wrap;font-size:12px;color:#58606e}
.bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
.bar select,.bar input[type=range]{font-size:12px}
.bar .val{font-family:ui-monospace,Consolas,monospace;color:#1f5fd0;min-width:44px}
.bar .sep{width:1px;height:16px;background:#d8dce2}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
.panels{flex:1 1 auto;min-height:0;display:grid;grid-template-columns:340px 1fr;gap:1px;background:#ccd3db}
.request{margin:0;padding:10px;overflow:auto;background:#1d2530;color:#d6e2ee;
font:11px/1.55 ui-monospace,Consolas,monospace;white-space:pre-wrap}
#map{min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<label>名称前缀 <input id="prefix" value="永定" size="6"></label>
<button id="send" type="button">发送请求</button>
<span class="sep"></span>
<span id="info">等待发送</span>
</div>
<div class="panels">
<pre id="request" class="request"></pre>
<div id="map"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
'use strict';
if (typeof ol === 'undefined') {
document.getElementById('map').textContent = 'OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net';
return;
}
var waters = [
{name: '永定河', waterway: 'river', ring: [[116.20, 39.90], [116.24, 39.92], [116.27, 39.90], [116.30, 39.88]]},
{name: '永定新河', waterway: 'river', ring: [[116.30, 39.88], [116.35, 39.86], [116.40, 39.85], [116.45, 39.84]]},
{name: '潮白河', waterway: 'river', ring: [[116.60, 40.00], [116.65, 39.95], [116.70, 39.90], [116.75, 39.85]]},
{name: '温榆河', waterway: 'river', ring: [[116.45, 40.05], [116.50, 40.02], [116.55, 39.98], [116.58, 39.95]]},
{name: '昆明湖', waterway: 'lake', ring: [[116.255, 39.985], [116.285, 39.985], [116.285, 40.005], [116.255, 40.005]]}
];
var vectorSource = new ol.source.Vector();
var vector = new ol.layer.Vector({
source: vectorSource,
style: new ol.style.Style({
stroke: new ol.style.Stroke({color: 'rgba(21, 101, 192, 0.95)', width: 3}),
fill: new ol.style.Fill({color: 'rgba(21, 101, 192, 0.15)'})
})
});
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}), opacity: 0.6}),
vector
],
view: new ol.View({
center: ol.proj.fromLonLat([116.42, 39.93]),
zoom: 11
})
});
function sendRequest() {
var prefix = document.getElementById('prefix').value || '永定';
var request = new ol.format.WFS().writeGetFeature({
srsName: 'EPSG:3857',
featureNS: 'http://demo.local/water',
featurePrefix: 'demo',
featureTypes: ['water'],
outputFormat: 'application/json',
filter: ol.format.filter.and(
ol.format.filter.like('name', prefix + '*'),
ol.format.filter.equalTo('waterway', 'river')
)
});
var xml = new XMLSerializer().serializeToString(request);
document.getElementById('request').textContent = xml;
var matches = waters.filter(function (water) {
return water.waterway === 'river' && water.name.indexOf(prefix) === 0;
});
var features = matches.map(function (water) {
return new ol.Feature({
geometry: new ol.geom.LineString(water.ring.map(function (p) {
return ol.proj.fromLonLat(p);
})),
name: water.name,
waterway: water.waterway
});
});
vectorSource.clear();
vectorSource.addFeatures(features);
document.getElementById('info').textContent =
'命中 ' + features.length + ' 条河流';
if (features.length) {
map.getView().fit(vectorSource.getExtent(), {padding: [60, 60, 60, 60]});
}
}
document.getElementById('send').addEventListener('click', sendRequest);
document.getElementById('prefix').addEventListener('keydown', function (evt) {
if (evt.key === 'Enter') {
sendRequest();
}
});
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.