下面是主要代码
图层和绘制多边形的代码就不贴了
draw.on('drawend',function (e) {let featureRequest = new ol.format.WFS().writeGetFeature({//坐标系统srsName:'EPSG:4326',//命名空间featureNS:'http://www.openplans.org/spearfish',//工作区名称featurePrefix:'sf',//查询图层featureTypes:['sf:roads','sf:bugsites'],outputFormat:'application/json',filter: ol.format.filter.intersects('the_geom',e.feature.getGeometry())});//WFS服务地址fetch('http://localhost:8080/geoserver/wfs',{method:'POST',body:new XMLSerializer().serializeToString(featureRequest),}).then(function (response) {return response.json();}).then(function (json) {console.log(json.features);let feasarr =[];for(let i=0;i<json.totalFeatures;i++){let features=new ol.format.GeoJSON().readFeature(JSON.stringify(json.features[i]));feasarr.push(features);}VectorLayer.addFeatures(feasarr);map.removeInteraction(draw);console.log(VectorLayer.getFeatures())})});
featureTypes:['sf:roads','sf:bugsites'],这里 可以填多个于同一工作区的多个图层要素这样就能查出与多个要素相交的部分