getFeatures method and featureInfo templates

To avoid surprises for application developers, this change
creates a new getFeatures method. So it is clear now beforehand
whether features or feature info markup is returned. The result
is now also grouped by layer, so application developers always
have a link between a layer and the feature info it returns.

To make getFeatureInfo return markup for vector layers, this
change also adds a featureInfoFunction property to the vector
layer, which gives developers full control over how features are
rendered to feature info markup.
This commit is contained in:
ahocevar
2013-05-29 19:51:12 -06:00
parent 34becd6871
commit 77d22c4038
9 changed files with 155 additions and 37 deletions
@@ -193,17 +193,31 @@ ol.renderer.canvas.VectorLayer.prototype.getTransform = function() {
/**
* @param {ol.Pixel} pixel Pixel coordinate relative to the map viewport.
* @param {function(Array.<ol.Feature|string>)} success Callback for
* successful queries. The passed argument is the resulting feature
* information. Layers that are able to provide attribute data will put
* ol.Feature instances, other layers will put a string which can either
* be plain text or markup.
* @param {function(string, ol.layer.Layer)} success Callback for
* successful queries. The passed arguments are the resulting feature
* information and the layer.
*/
ol.renderer.canvas.VectorLayer.prototype.getFeatureInfoForPixel =
function(pixel, success) {
var callback = function(features, layer) {
success(layer.getFeatureInfoFunction()(features), layer);
};
this.getFeaturesForPixel(pixel, callback);
};
/**
* @param {ol.Pixel} pixel Pixel coordinate relative to the map viewport.
* @param {function(Array.<ol.Feature>, ol.layer.Layer)} success Callback for
* successful queries. The passed arguments are the resulting features
* and the layer.
*/
ol.renderer.canvas.VectorLayer.prototype.getFeaturesForPixel =
function(pixel, success) {
var map = this.getMap();
var result = [];
var layer = this.getLayer();
var location = map.getCoordinateFromPixel(pixel);
var tileCoord = this.tileGrid_.getTileCoordForCoordAndResolution(
location, this.getMap().getView().getView2D().getResolution());
@@ -218,7 +232,7 @@ ol.renderer.canvas.VectorLayer.prototype.getFeatureInfoForPixel =
var locationMax = [location[0] + halfMaxWidth, location[1] + halfMaxHeight];
var locationBbox = ol.extent.boundingExtent([locationMin, locationMax]);
var filter = new ol.filter.Extent(locationBbox);
var candidates = this.getLayer().getFeatures(filter);
var candidates = layer.getFeatures(filter);
var candidate, geom, type, symbolBounds, symbolSize, halfWidth, halfHeight,
coordinates, j;
@@ -261,7 +275,7 @@ ol.renderer.canvas.VectorLayer.prototype.getFeatureInfoForPixel =
}
}
}
goog.global.setTimeout(function() { success(result); }, 0);
goog.global.setTimeout(function() { success(result, layer); }, 0);
};
+43 -4
View File
@@ -1,6 +1,7 @@
goog.provide('ol.renderer.Map');
goog.require('goog.Disposable');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.dispose');
goog.require('goog.functions');
@@ -110,22 +111,60 @@ ol.renderer.Map.prototype.getCanvas = goog.functions.NULL;
*/
ol.renderer.Map.prototype.getFeatureInfoForPixel =
function(pixel, layers, success, opt_error) {
var layer, layerRenderer;
var featureInfo = [];
var numLayers = layers.length;
var callback = function(layerFeatureInfo) {
featureInfo.push.apply(featureInfo, layerFeatureInfo);
var featureInfo = new Array(numLayers);
var callback = function(layerFeatureInfo, layer) {
featureInfo[goog.array.indexOf(layers, layer)] = layerFeatureInfo;
--numLayers;
if (!numLayers) {
success(featureInfo);
}
};
var layer, layerRenderer;
for (var i = 0; i < numLayers; ++i) {
layer = layers[i];
layerRenderer = this.getLayerRenderer(layer);
if (goog.isFunction(layerRenderer.getFeatureInfoForPixel)) {
layerRenderer.getFeatureInfoForPixel(pixel, callback, opt_error);
} else {
--numLayers;
}
}
};
/**
* @param {ol.Pixel} pixel Pixel coordinate relative to the map viewport.
* @param {Array.<ol.layer.Layer>} layers Layers to query.
* @param {function(Array.<ol.Feature|string>)} success Callback for
* successful queries. The passed argument is the resulting feature
* information. Layers that are able to provide attribute data will put
* ol.Feature instances, other layers will put a string which can either
* be plain text or markup.
* @param {function(Object)=} opt_error Callback for unsuccessful
* queries.
*/
ol.renderer.Map.prototype.getFeaturesForPixel =
function(pixel, layers, success, opt_error) {
var numLayers = layers.length;
var features = new Array(numLayers);
var callback = function(layerFeatures, layer) {
features[goog.array.indexOf(layers, layer)] = layerFeatures;
--numLayers;
if (!numLayers) {
success(features);
}
};
var layer, layerRenderer;
for (var i = 0; i < numLayers; ++i) {
layer = layers[i];
layerRenderer = this.getLayerRenderer(layer);
if (goog.isFunction(layerRenderer.getFeaturesForPixel)) {
layerRenderer.getFeaturesForPixel(pixel, callback, opt_error);
} else {
--numLayers;
}
}
};