Method for getting both feature id and data.
Having to call two methods to get complete feature information (id and data) is cumbersome. The `getFeatureInfo` method returns an object with both feature id and data.
This commit is contained in:
@@ -33,7 +33,7 @@
|
||||
* var control = new OpenLayers.Control.UTFGrid({
|
||||
* layers: [world_utfgrid],
|
||||
* handlerMode: 'move',
|
||||
* callback: function(dataLookup) {
|
||||
* callback: function(infoLookup) {
|
||||
* // do something with returned data
|
||||
*
|
||||
* }
|
||||
@@ -175,14 +175,14 @@ OpenLayers.Control.UTFGrid = OpenLayers.Class(OpenLayers.Control, {
|
||||
|
||||
var layers = this.findLayers();
|
||||
if (layers.length > 0) {
|
||||
var dataLookup = {};
|
||||
var infoLookup = {};
|
||||
var layer, idx;
|
||||
for (var i=0, len=layers.length; i<len; i++) {
|
||||
layer = layers[i];
|
||||
idx = this.map.layers.indexOf(layer);
|
||||
dataLookup[idx] = layer.getFeatureData(lonLat);
|
||||
infoLookup[idx] = layer.getFeatureInfo(lonLat);
|
||||
}
|
||||
this.callback(dataLookup); // perhaps pass tile, lonLat?
|
||||
this.callback(infoLookup); // perhaps pass tile, lonLat?
|
||||
}
|
||||
},
|
||||
|
||||
@@ -192,12 +192,12 @@ OpenLayers.Control.UTFGrid = OpenLayers.Class(OpenLayers.Control, {
|
||||
* includes data in one of the configured UTFGrid layers.
|
||||
*
|
||||
* Parameters:
|
||||
* dataLookup - {Object} Keys of this object are layer indexes and can be
|
||||
* infoLookup - {Object} Keys of this object are layer indexes and can be
|
||||
* used to resolve a layer in the map.layers array. The structure of
|
||||
* the property values depend on the data included in the underlying
|
||||
* UTFGrid and may be any valid JSON type.
|
||||
*/
|
||||
callback: function(dataLookup) {
|
||||
callback: function(infoLookup) {
|
||||
// to be provided in the constructor
|
||||
},
|
||||
|
||||
|
||||
@@ -237,22 +237,26 @@ OpenLayers.Layer.UTFGrid = OpenLayers.Class(OpenLayers.Layer.Grid, {
|
||||
},
|
||||
|
||||
/**
|
||||
* APIProperty: getFeatureData
|
||||
* Get feature data from UTFGrid associated with a map location.
|
||||
* APIProperty: getFeatureInfo
|
||||
* Get details about a feature associated with a map location. The object
|
||||
* returned will have id and data properties. If the given location
|
||||
* doesn't correspond to a feature, null will be returned.
|
||||
*
|
||||
* Parameters:
|
||||
* location - {<OpenLayers.LonLat>} map location
|
||||
*
|
||||
* Returns:
|
||||
* {Object} The UTFGrid data corresponding to the given map location.
|
||||
* {Object} Object representing the feature id and UTFGrid data
|
||||
* corresponding to the given map location. Returns null if the given
|
||||
* location doesn't hit a feature.
|
||||
*/
|
||||
getFeatureData: function(location) {
|
||||
var data;
|
||||
var info = this.getTileInfo(location);
|
||||
if (info.tile) {
|
||||
data = info.tile.getFeatureData(info.i, info.j);
|
||||
getFeatureInfo: function(location) {
|
||||
var info = null;
|
||||
var tileInfo = this.getTileInfo(location);
|
||||
if (tileInfo.tile) {
|
||||
info = tileInfo.tile.getFeatureInfo(tileInfo.i, tileInfo.j);
|
||||
}
|
||||
return data;
|
||||
return info;
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -263,10 +267,11 @@ OpenLayers.Layer.UTFGrid = OpenLayers.Class(OpenLayers.Layer.Grid, {
|
||||
* location - {<OpenLayers.LonLat>} map location
|
||||
*
|
||||
* Returns:
|
||||
* {Object} The feature identifier corresponding to the given map location.
|
||||
* {String} The feature identifier corresponding to the given map location.
|
||||
* Returns null if the location doesn't hit a feature.
|
||||
*/
|
||||
getFeatureId: function(location) {
|
||||
var id;
|
||||
var id = null;
|
||||
var info = this.getTileInfo(location);
|
||||
if (info.tile) {
|
||||
id = info.tile.getFeatureId(info.i, info.j);
|
||||
|
||||
@@ -146,22 +146,29 @@ OpenLayers.Tile.UTFGrid = OpenLayers.Class(OpenLayers.Tile, {
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: getFeatureData
|
||||
* Get feature data associated with a pixel offset.
|
||||
* Method: getFeatureInfo
|
||||
* Get feature information associated with a pixel offset. If the pixel
|
||||
* offset corresponds to a feature, the returned object will have id
|
||||
* and data properties. Otherwise, null will be returned.
|
||||
*
|
||||
*
|
||||
* Parameters:
|
||||
* i - {Number} X-axis pixel offset (from top left of tile)
|
||||
* j - {Number} Y-axis pixel offset (from top left of tile)
|
||||
*
|
||||
* Returns:
|
||||
* {Object} The UTFGrid data corresponding to the given pixel offset.
|
||||
* {Object} Object with feature id and data properties corresponding to the
|
||||
* given pixel offset.
|
||||
*/
|
||||
getFeatureData: function(i, j) {
|
||||
var data;
|
||||
getFeatureInfo: function(i, j) {
|
||||
var info = null;
|
||||
if (this.json) {
|
||||
data = this.json.data[this.getFeatureId(i, j)];
|
||||
var id = this.getFeatureId(i, j);
|
||||
if (id !== null) {
|
||||
info = {id: id, data: this.json.data[id]};
|
||||
}
|
||||
}
|
||||
return data;
|
||||
return info;
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -174,16 +181,20 @@ OpenLayers.Tile.UTFGrid = OpenLayers.Class(OpenLayers.Tile, {
|
||||
*
|
||||
* Returns:
|
||||
* {Object} The feature identifier corresponding to the given pixel offset.
|
||||
* Returns null if pixel doesn't correspond to a feature.
|
||||
*/
|
||||
getFeatureId: function(i, j) {
|
||||
var id;
|
||||
var id = null;
|
||||
if (this.json) {
|
||||
var resolution = this.utfgridResolution;
|
||||
var row = Math.floor(j / resolution);
|
||||
var col = Math.floor(i / resolution);
|
||||
var charCode = this.json.grid[row].charCodeAt(col);
|
||||
var index = this.indexFromCharCode(charCode);
|
||||
id = this.json.keys[index];
|
||||
var keys = this.json.keys;
|
||||
if (!isNaN(index) && (index in keys)) {
|
||||
id = keys[index];
|
||||
}
|
||||
}
|
||||
return id;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user