绘制多边形并通过其选择要素:
draw.on('drawend', function(event) {
delaySelectActivate();
selectedFeatures.clear();
var polygon = event.feature.getGeometry();
var features1 = vectorSource.getFeatures();
for (var i = 0 ; i < features1.length; i++){
if(polygon.intersectsExtent( features1[i].getGeometry().getExtent() )){
selectedFeatures.push(features1[i]);
}
}
});
返回以下结果: 由于某种原因,西班牙也被选中,即使看起来与多边形的范围相差甚远? 这是一个选择拖动框的示例:
这是相关的拖动框js:
dragBox.on('boxend', function() {
// features that intersect the box are added to the collection of
// selected features
drawingSource.clear();
var extent = dragBox.getGeometry().getExtent();
vectorSource.forEachFeatureIntersectingExtent(extent, function(feature) {
selectedFeatures.push(feature);
});
});
我尝试过的事情:
我认为这是因为绘制的多边形的范围不准确。但是,当使用拖动框时,看起来也是多边形的要素范围又如何呢?
我遵循了本教程:Selecting features by drawing polygons in openLayers 3
答案 0 :(得分:1)
使用forEachFeatureIntersectingExtent
方法似乎确实更准确:
draw.on('drawend', function(event) {
delaySelectActivate();
selectedFeatures.clear();
var polygon = event.feature.getGeometry();
vectorSource.forEachFeatureIntersectingExtent(polygon.getExtent(), function(feature) {
selectedFeatures.push(feature);
});
});
我想本教程的作者使用了for循环,因为他是相交的点而不是vectorSource。