Allow forEachFeatureAtPixel callback to break out of loop

This commit is contained in:
Tom Payne
2013-12-02 16:24:11 +01:00
parent 1a9d19a2fb
commit d7591594ca
5 changed files with 77 additions and 28 deletions
@@ -85,14 +85,16 @@ ol.renderer.canvas.VectorLayer.prototype.composeFrame =
* @inheritDoc
*/
ol.renderer.canvas.VectorLayer.prototype.forEachFeatureAtPixel =
function(pixel, callback) {
if (!goog.isNull(this.replayGroup_)) {
function(pixel, callback, opt_obj) {
if (goog.isNull(this.replayGroup_)) {
return undefined;
} else {
goog.asserts.assert(!ol.extent.isEmpty(this.renderedExtent_));
goog.asserts.assert(!isNaN(this.renderedResolution_));
var coordinate = this.getMap().getCoordinateFromPixel(pixel);
var renderGeometryFunction = this.getRenderGeometryFunction_();
goog.asserts.assert(goog.isFunction(renderGeometryFunction));
this.replayGroup_.forEachGeometryAtCoordinate(this.renderedExtent_,
return this.replayGroup_.forEachGeometryAtCoordinate(this.renderedExtent_,
this.renderedResolution_, coordinate, renderGeometryFunction,
/**
* @param {ol.geom.Geometry} geometry Geometry.
@@ -101,7 +103,7 @@ ol.renderer.canvas.VectorLayer.prototype.forEachFeatureAtPixel =
function(geometry, data) {
var feature = /** @type {ol.Feature} */ (data);
goog.asserts.assert(goog.isDef(feature));
callback(feature);
return callback.call(opt_obj, feature);
});
}
};
+4 -1
View File
@@ -44,7 +44,10 @@ goog.inherits(ol.renderer.Layer, goog.Disposable);
/**
* @param {ol.Pixel} pixel Pixel.
* @param {function(ol.Feature)} callback Feature callback.
* @param {function(this: S, ol.Feature): T} callback Feature callback.
* @param {S=} opt_obj Scope.
* @return {T|undefined} Callback result.
* @template S,T
*/
ol.renderer.Layer.prototype.forEachFeatureAtPixel = goog.nullFunction;
+22 -5
View File
@@ -80,13 +80,30 @@ ol.renderer.Map.prototype.disposeInternal = function() {
/**
* @param {ol.Pixel} pixel Pixel.
* @param {function(ol.Feature)} callback Feature callback.
* @param {function(this: S, ol.Feature): T} callback Feature callback.
* @param {S=} opt_obj Scope.
* @return {T|undefined} Callback result.
* @template S,T
*/
ol.renderer.Map.prototype.forEachFeatureAtPixel =
function(pixel, callback) {
goog.object.forEach(this.layerRenderers_, function(layerRenderer) {
layerRenderer.forEachFeatureAtPixel(pixel, callback);
});
function(pixel, callback, opt_obj) {
var layers = this.map_.getLayers();
if (goog.isDef(layers)) {
var layersArray = layers.getArray();
var i;
for (i = layersArray.length - 1; i >= 0; --i) {
var layer = layersArray[i];
if (layer.getVisible()) {
var layerRenderer = this.getLayerRenderer(layer);
var result =
layerRenderer.forEachFeatureAtPixel(pixel, callback, opt_obj);
if (result) {
return result;
}
}
}
}
return undefined;
};