ol之filter過濾獲取feature
使用場景:
通過geoserver的省界服務查詢對應省的要素,定位到對應位置
代碼: sfCode 為省份編碼
queryLocation (sfCode) { let featureRequest = new ol.format.WFS().writeGetFeature({ ssName: 'EPSG:3857', featureNS: 'gistone', // 命名空間 URI featurePrefix: 'njs', // 工作區名稱 featureTypes: ['sj'], // 查詢圖層,可以同一個工作區下多個圖層,逗號隔開 outputFormat: 'application/json', filter: ol.format.filter.like('code', sfCode) }) fetch('http://localhost:8080/geoserver/njs/wfs', { method: 'POST', body: new XMLSerializer().serializeToString(featureRequest) }).then((res) => { return res.json() }).then(res => { if (res.totalFeatures !== 0) { let vectorSource = new ol.source.Vector() let features = new ol.format.GeoJSON().readFeatures(res) vectorSource.addFeatures(features) let extent = ol.proj.transformExtent(vectorSource.getExtent(), 'EPSG:4326', 'EPSG:3857') this.view.fit(extent) } }) },
注意事項:
1.在查詢字段設置錯誤時,可能會報錯: json格式不正確 <
2.地圖服務中字段名和查詢字段中的數據不能為中文。
3.‘http://localhost:8080/geoserver/njs/wfs’中njs為工作空間名稱
鑽研不易,轉載請注明出處、、、、、、