从Openlayers 3视口获取所有功能

Get all features from the Openlayers 3 viewport

本文关键字:功能 获取 视口 Openlayers      更新时间:2023-09-26

我正在努力找出Openlayers 3中一个层上可见的所有功能(视口(。

如果我在地图上添加一个点击事件,我就能找到一个单一的功能,如下所示。但我无法找到视口中可见的所有功能。有人能帮忙吗?

map.on('click', function(evt) {
        var feature = map.forEachFeatureAtPixel(evt.pixel,
            function(feature, layer) {
                return feature;
            });
});

我建议首先获得视图的范围:

var extent = yourMap.getView().calculateExtent(yourMmap.getSize());

然后获得这个范围内的所有功能:

yourVectorSource.forEachFeatureInExtent(extent, function(feature){
    // do something 
});