Store the feature's commonly used id

To not clobber the feature's attributes, this is a separate
member property.
This commit is contained in:
ahocevar
2013-05-23 11:16:17 -05:00
parent d1beaff8e4
commit 8c0f1f979a
6 changed files with 42 additions and 1 deletions

View File

@@ -48,7 +48,7 @@ map.on(['click', 'mousemove'], function(evt) {
success: function(features) { 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].getFeatureId() + ': ' + features[i].get('name'));
} }
document.getElementById('info').innerHTML = info.join(', ') || '&nbsp;'; document.getElementById('info').innerHTML = info.join(', ') || '&nbsp;';
} }

View File

@@ -1,7 +1,9 @@
@exportSymbol ol.Feature @exportSymbol ol.Feature
@exportProperty ol.Feature.prototype.get @exportProperty ol.Feature.prototype.get
@exportProperty ol.Feature.prototype.getAttributes @exportProperty ol.Feature.prototype.getAttributes
@exportProperty ol.Feature.prototype.getFeatureId
@exportProperty ol.Feature.prototype.getGeometry @exportProperty ol.Feature.prototype.getGeometry
@exportProperty ol.Feature.prototype.set @exportProperty ol.Feature.prototype.set
@exportProperty ol.Feature.prototype.setFeatureId
@exportProperty ol.Feature.prototype.setGeometry @exportProperty ol.Feature.prototype.setGeometry
@exportProperty ol.Feature.prototype.setSymbolizers @exportProperty ol.Feature.prototype.setSymbolizers

View File

@@ -14,6 +14,12 @@ ol.Feature = function(opt_values) {
goog.base(this, opt_values); goog.base(this, opt_values);
/**
* @type {string|undefined}
* @private
*/
this.featureId_;
/** /**
* @type {string|undefined} * @type {string|undefined}
* @private * @private
@@ -46,6 +52,17 @@ ol.Feature.prototype.getAttributes = function() {
}; };
/**
* Returns the feature's commonly used identifier. This identifier is usually
* the unique id in the source store.
*
* @return {string|undefined} The feature's identifier.
*/
ol.Feature.prototype.getFeatureId = function() {
return this.featureId_;
};
/** /**
* @return {ol.geom.Geometry} The geometry (or null if none). * @return {ol.geom.Geometry} The geometry (or null if none).
*/ */
@@ -85,6 +102,17 @@ ol.Feature.prototype.set = function(key, value) {
}; };
/**
* Set the feature's commonly used identifier. This identifier is usually the
* unique id in the source store.
*
* @param {string} featureId The feature's identifier.
*/
ol.Feature.prototype.setFeatureId = function(featureId) {
this.featureId_ = featureId;
};
/** /**
* @param {ol.geom.Geometry} geometry The geometry. * @param {ol.geom.Geometry} geometry The geometry.
*/ */

View File

@@ -129,6 +129,9 @@ ol.parser.GeoJSON.prototype.parseFeature_ = function(json, opt_options) {
geometry = null, geometry = null,
options = opt_options || {}; options = opt_options || {};
var feature = new ol.Feature(json.properties); var feature = new ol.Feature(json.properties);
if (goog.isDef(json.id)) {
feature.setFeatureId(json.id);
}
if (geomJson) { if (geomJson) {
var type = geomJson.type; var type = geomJson.type;
var callback = options.callback; var callback = options.callback;

View File

@@ -16,6 +16,12 @@ describe('ol.Feature', function() {
expect(feature.get('foo')).to.be('bar'); expect(feature.get('foo')).to.be('bar');
}); });
it('can store the feature\'s commonly used id', function() {
var feature = new ol.Feature();
feature.setFeatureId('foo');
expect(feature.getFeatureId()).to.be('foo');
});
it('will set the default geometry', function() { it('will set the default geometry', function() {
var feature = new ol.Feature({ var feature = new ol.Feature({
loc: new ol.geom.Point([10, 20]), loc: new ol.geom.Point([10, 20]),

View File

@@ -151,6 +151,7 @@ describe('ol.parser.GeoJSON', function() {
var first = result[0]; var first = result[0];
expect(first).to.be.a(ol.Feature); expect(first).to.be.a(ol.Feature);
expect(first.get('name')).to.be('Afghanistan'); expect(first.get('name')).to.be('Afghanistan');
expect(first.getFeatureId()).to.be('AFG');
var firstGeom = first.getGeometry(); var firstGeom = first.getGeometry();
expect(firstGeom).to.be.a(ol.geom.Polygon); expect(firstGeom).to.be.a(ol.geom.Polygon);
expect(ol.extent.equals(firstGeom.getBounds(), expect(ol.extent.equals(firstGeom.getBounds(),
@@ -160,6 +161,7 @@ describe('ol.parser.GeoJSON', function() {
var last = result[178]; var last = result[178];
expect(last).to.be.a(ol.Feature); expect(last).to.be.a(ol.Feature);
expect(last.get('name')).to.be('Zimbabwe'); expect(last.get('name')).to.be('Zimbabwe');
expect(last.getFeatureId()).to.be('ZWE');
var lastGeom = last.getGeometry(); var lastGeom = last.getGeometry();
expect(lastGeom).to.be.a(ol.geom.Polygon); expect(lastGeom).to.be.a(ol.geom.Polygon);
expect(ol.extent.equals(lastGeom.getBounds(), expect(ol.extent.equals(lastGeom.getBounds(),