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:
@@ -43,12 +43,17 @@ var map = new ol.Map({
|
|||||||
});
|
});
|
||||||
|
|
||||||
map.on('mousemove', function(evt) {
|
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 = [];
|
var info = [];
|
||||||
for (var i = 0, ii = features.length; i < ii; ++i) {
|
for (var i = 0, ii = features.length; i < ii; ++i) {
|
||||||
info.push(features[i].get('name'));
|
info.push(features[i].get('name'));
|
||||||
}
|
}
|
||||||
document.getElementById('info').innerHTML = info.join(', ') || ' ';
|
document.getElementById('info').innerHTML = info.join(', ') || ' ';
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,20 @@
|
|||||||
* @property {ol.ProjectionLike} projection Projection.
|
* @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.
|
* Object literal with config options for the map.
|
||||||
* @typedef {Object} ol.MapOptions
|
* @typedef {Object} ol.MapOptions
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
@exportProperty ol.Map.prototype.addLayer
|
@exportProperty ol.Map.prototype.addLayer
|
||||||
@exportProperty ol.Map.prototype.addPreRenderFunction
|
@exportProperty ol.Map.prototype.addPreRenderFunction
|
||||||
@exportProperty ol.Map.prototype.addPreRenderFunctions
|
@exportProperty ol.Map.prototype.addPreRenderFunctions
|
||||||
@exportProperty ol.Map.prototype.getFeatureInfoForPixel
|
@exportProperty ol.Map.prototype.getFeatureInfo
|
||||||
@exportProperty ol.Map.prototype.getInteractions
|
@exportProperty ol.Map.prototype.getInteractions
|
||||||
@exportProperty ol.Map.prototype.getRenderer
|
@exportProperty ol.Map.prototype.getRenderer
|
||||||
@exportProperty ol.Map.prototype.removeLayer
|
@exportProperty ol.Map.prototype.removeLayer
|
||||||
|
|||||||
@@ -430,17 +430,13 @@ ol.Map.prototype.getCoordinateFromPixel = function(pixel) {
|
|||||||
/**
|
/**
|
||||||
* Get feature information for a pixel on the map.
|
* Get feature information for a pixel on the map.
|
||||||
*
|
*
|
||||||
* @param {ol.Pixel} pixel Pixel coordinate relative to the map viewport.
|
* @param {ol.GetFeatureInfoOptions} options Options.
|
||||||
* @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.
|
|
||||||
*/
|
*/
|
||||||
ol.Map.prototype.getFeatureInfoForPixel = function(pixel, opt_layers) {
|
ol.Map.prototype.getFeatureInfo = function(options) {
|
||||||
var layers = goog.isDefAndNotNull(opt_layers) ?
|
var layers = goog.isDefAndNotNull(options.layers) ?
|
||||||
opt_layers : this.getLayers().getArray();
|
options.layers : this.getLayers().getArray();
|
||||||
return this.getRenderer().getFeatureInfoForPixel(pixel, layers);
|
this.getRenderer().getFeatureInfoForPixel(
|
||||||
|
options.pixel, layers, options.success, options.error);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -190,10 +190,14 @@ ol.renderer.canvas.VectorLayer.prototype.getTransform = function() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {ol.Pixel} pixel Pixel coordinate relative to the map viewport.
|
* @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 =
|
ol.renderer.canvas.VectorLayer.prototype.getFeatureInfoForPixel =
|
||||||
function(pixel) {
|
function(pixel, success) {
|
||||||
// TODO adjust pixel tolerance for applied styles
|
// TODO adjust pixel tolerance for applied styles
|
||||||
var minPixel = new ol.Pixel(pixel.x - 1, pixel.y - 1);
|
var minPixel = new ol.Pixel(pixel.x - 1, pixel.y - 1);
|
||||||
var maxPixel = 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);
|
result.push(candidate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
goog.global.setTimeout(function() { success(result); }, 0);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -106,23 +106,34 @@ ol.renderer.Map.prototype.getCanvas = goog.functions.NULL;
|
|||||||
/**
|
/**
|
||||||
* @param {ol.Pixel} pixel Pixel coordinate relative to the map viewport.
|
* @param {ol.Pixel} pixel Pixel coordinate relative to the map viewport.
|
||||||
* @param {Array.<ol.layer.Layer>} layers Layers to query.
|
* @param {Array.<ol.layer.Layer>} layers Layers to query.
|
||||||
* @return {Array.<ol.Feature|string>} Feature information. Layers that are
|
* @param {function(Array.<ol.Feature|string>)} success Callback for
|
||||||
* able to return attribute data will return ol.Feature instances, other
|
* successful queries. The passed argument is the resulting feature
|
||||||
* layers will return a string which can either be plain text or markup.
|
* 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 =
|
ol.renderer.Map.prototype.getFeatureInfoForPixel =
|
||||||
function(pixel, layers) {
|
function(pixel, layers, success, opt_error) {
|
||||||
var layer, layerRenderer;
|
var layer, layerRenderer;
|
||||||
var featureInfo = [];
|
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];
|
layer = layers[i];
|
||||||
layerRenderer = this.getLayerRenderer(layer);
|
layerRenderer = this.getLayerRenderer(layer);
|
||||||
if (goog.isFunction(layerRenderer.getFeatureInfoForPixel)) {
|
if (goog.isFunction(layerRenderer.getFeatureInfoForPixel)) {
|
||||||
featureInfo.push.apply(featureInfo,
|
layerRenderer.getFeatureInfoForPixel(pixel, callback, opt_error);
|
||||||
layerRenderer.getFeatureInfoForPixel(pixel));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return featureInfo;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user