Asynchronous API for map#getFeatureInfo

This change allows us to use feature info services on the
server. It will also be useful for the GetFeature control to
get results from a WFS.
This commit is contained in:
ahocevar
2013-05-09 22:26:55 +02:00
parent fa1eb354f2
commit 7b256c3ec6
6 changed files with 58 additions and 28 deletions

View File

@@ -43,12 +43,17 @@ var map = new ol.Map({
});
map.on('mousemove', function(evt) {
var features = map.getFeatureInfoForPixel(evt.getPixel(), [vector]);
var features = map.getFeatureInfo({
pixel: evt.getPixel(),
layers: [vector],
success: function(features) {
var info = [];
for (var i = 0, ii = features.length; i < ii; ++i) {
info.push(features[i].get('name'));
}
document.getElementById('info').innerHTML = info.join(', ') || '&nbsp;';
}
});
});

View File

@@ -5,6 +5,20 @@
* @property {ol.ProjectionLike} projection Projection.
*/
/**
* @typedef {Object} ol.GetFeatureInfoOptions
* @property {ol.Pixel} pixel Pixel coordinate relative to the map viewport.
* @property {Array.<ol.layer.Layer>|undefined} layers Layers to restrict the
* query to. All layers will be queried if not provided.
* @property {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.
* @property {function(Object)|undefined} error Callback for unsuccessful
* queries.
*/
/**
* Object literal with config options for the map.
* @typedef {Object} ol.MapOptions

View File

@@ -2,7 +2,7 @@
@exportProperty ol.Map.prototype.addLayer
@exportProperty ol.Map.prototype.addPreRenderFunction
@exportProperty ol.Map.prototype.addPreRenderFunctions
@exportProperty ol.Map.prototype.getFeatureInfoForPixel
@exportProperty ol.Map.prototype.getFeatureInfo
@exportProperty ol.Map.prototype.getInteractions
@exportProperty ol.Map.prototype.getRenderer
@exportProperty ol.Map.prototype.removeLayer

View File

@@ -430,17 +430,13 @@ ol.Map.prototype.getCoordinateFromPixel = function(pixel) {
/**
* Get feature information for a pixel on the map.
*
* @param {ol.Pixel} pixel Pixel coordinate relative to the map viewport.
* @param {Array.<ol.layer.Layer>=} opt_layers Layers to restrict the query to.
* All layers will be queried if not provided.
* @return {Array.<ol.Feature|string>} Feature information. Layers that are
* able to return attribute data will return ol.Feature instances, other
* layers will return a string which can either be plain text or markup.
* @param {ol.GetFeatureInfoOptions} options Options.
*/
ol.Map.prototype.getFeatureInfoForPixel = function(pixel, opt_layers) {
var layers = goog.isDefAndNotNull(opt_layers) ?
opt_layers : this.getLayers().getArray();
return this.getRenderer().getFeatureInfoForPixel(pixel, layers);
ol.Map.prototype.getFeatureInfo = function(options) {
var layers = goog.isDefAndNotNull(options.layers) ?
options.layers : this.getLayers().getArray();
this.getRenderer().getFeatureInfoForPixel(
options.pixel, layers, options.success, options.error);
};

View File

@@ -190,10 +190,14 @@ ol.renderer.canvas.VectorLayer.prototype.getTransform = function() {
/**
* @param {ol.Pixel} pixel Pixel coordinate relative to the map viewport.
* @return {Array.<ol.Feature>} Features at the pixel location.
* @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.
*/
ol.renderer.canvas.VectorLayer.prototype.getFeatureInfoForPixel =
function(pixel) {
function(pixel, success) {
// TODO adjust pixel tolerance for applied styles
var minPixel = new ol.Pixel(pixel.x - 1, pixel.y - 1);
var maxPixel = new ol.Pixel(pixel.x + 1, pixel.y + 1);
@@ -230,7 +234,7 @@ ol.renderer.canvas.VectorLayer.prototype.getFeatureInfoForPixel =
result.push(candidate);
}
}
return result;
goog.global.setTimeout(function() { success(result); }, 0);
};

View File

@@ -106,23 +106,34 @@ ol.renderer.Map.prototype.getCanvas = goog.functions.NULL;
/**
* @param {ol.Pixel} pixel Pixel coordinate relative to the map viewport.
* @param {Array.<ol.layer.Layer>} layers Layers to query.
* @return {Array.<ol.Feature|string>} Feature information. Layers that are
* able to return attribute data will return ol.Feature instances, other
* layers will return a string which can either be plain text or markup.
* @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.getFeatureInfoForPixel =
function(pixel, layers) {
function(pixel, layers, success, opt_error) {
var layer, layerRenderer;
var featureInfo = [];
for (var i = 0, ii = layers.length; i < ii; ++i) {
var numLayers = layers.length;
var callback = function(layerFeatureInfo) {
featureInfo.push.apply(featureInfo, layerFeatureInfo);
--numLayers;
if (!numLayers) {
success(featureInfo);
}
};
for (var i = 0; i < numLayers; ++i) {
layer = layers[i];
layerRenderer = this.getLayerRenderer(layer);
if (goog.isFunction(layerRenderer.getFeatureInfoForPixel)) {
featureInfo.push.apply(featureInfo,
layerRenderer.getFeatureInfoForPixel(pixel));
layerRenderer.getFeatureInfoForPixel(pixel, callback, opt_error);
}
}
return featureInfo;
};