对于由任何 wms/wfs 服务器提供服务的 WMS 层,您可以使用以下内容执行 wms get 功能请求:
var url = myWMSLayer
.getSource()
.getGetFeatureInfoUrl(
evt.coordinate,
map.getView().getResolution(),
map.getView().getProjection(),
{
'INFO_FORMAT': 'application/json',
'propertyName': 'ATTR1,ATTR2,ATTR3'
}
);
这应该会给您传递的event.coordinate 中存在的任何功能。因此,您可能会取回给定点内存在的所有功能。
如果您只能访问服务器上的 WMS 请求,我认为这是您唯一的选择。
但是,如果您的服务器支持 WFS 请求并且您可以访问它们,您可以执行 wfs 请求以获取您想要的功能。类似于以下内容:
//here is the rectangle to search for fetaures
var extent [-8876804.07807116, 5368955.976007851, -8866790.827365803, 5374688.75312924];
$.ajax('http://demo.opengeo.org/geoserver/wfs', {
type: 'GET',
data: {
service: 'WFS',
version: '1.1.0',
request: 'GetFeature',
typename: 'mylayer',
srsname: 'EPSG:3857',
bbox: extent.join(',') + ',EPSG:3857'
}
}).done(function(resp){
//you may parse the responce back here
var formatWFS = new ol.format.WFS();
var feats = formatWFS.readFeatures(resp);
//now you can iterate through your features and get the attrs
for (var i=0;i<feats.length;i++){
console.log(feats[i].get('ATTR1'));
}
}).fail(function () {
alert("fail loading features");
});