From 227f3bae18bdd0b382009e0de3dc9303a9f64474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Wed, 17 Sep 2014 09:13:12 +0200 Subject: [PATCH] Add ol.source.Vector#forEachFeatureIntersectingExtent --- src/ol/source/vectorsource.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/ol/source/vectorsource.js b/src/ol/source/vectorsource.js index e82ade9da9..7720f746c0 100644 --- a/src/ol/source/vectorsource.js +++ b/src/ol/source/vectorsource.js @@ -247,6 +247,39 @@ ol.source.Vector.prototype.forEachFeatureInExtentAtResolution = }; +/** + * This function executes the `callback` function once for each feature + * intersecting the `extent` until `callback` returns a truthy value. When + * `callback` returns a truthy value the function immediately returns that + * value. Otherwise the function returns `undefined`. + * @param {ol.Extent} extent Extent. + * @param {function(this: T, ol.Feature): S} f Callback. + * @param {T=} opt_this The object to use as `this` in `f`. + * @return {S|undefined} + * @template T,S + * @api + */ +ol.source.Vector.prototype.forEachFeatureIntersectingExtent = + function(extent, f, opt_this) { + return this.forEachFeatureInExtent(extent, + /** + * @param {ol.Feature} feature Feature. + * @return {S|undefined} + * @template S + */ + function(feature) { + var geometry = feature.getGeometry(); + goog.asserts.assert(goog.isDefAndNotNull(geometry)); + if (geometry.intersectsExtent(extent)) { + var result = f.call(opt_this, feature); + if (result) { + return result; + } + } + }); +}; + + /** * @return {Array.} Features. * @api stable