Add/remove features in setMap

This commit is contained in:
Alexandre Dubé
2015-03-26 15:19:33 -04:00
parent 5bffa99e83
commit 1a5d7b85d2
+47 -24
View File
@@ -69,14 +69,6 @@ ol.interaction.Snap = function(opt_options) {
options.features : options.features :
null; null;
var features;
if (!goog.isNull(this.features_)) {
features = this.features_;
} else if (!goog.isNull(this.source_)) {
features = this.source_.getFeatures();
}
goog.asserts.assert(goog.isDef(features));
/** /**
* @type {ol.Collection.<goog.events.Key>} * @type {ol.Collection.<goog.events.Key>}
* @private * @private
@@ -120,8 +112,6 @@ ol.interaction.Snap = function(opt_options) {
'MultiPolygon': this.writeMultiPolygonGeometry_, 'MultiPolygon': this.writeMultiPolygonGeometry_,
'GeometryCollection': this.writeGeometryCollectionGeometry_ 'GeometryCollection': this.writeGeometryCollectionGeometry_
}; };
features.forEach(this.addFeature, this);
}; };
goog.inherits(ol.interaction.Snap, ol.interaction.Pointer); goog.inherits(ol.interaction.Snap, ol.interaction.Pointer);
@@ -173,6 +163,40 @@ ol.interaction.Snap.prototype.dragging_ = false;
ol.interaction.Snap.prototype.featurePending_ = null; ol.interaction.Snap.prototype.featurePending_ = null;
/**
* @param {ol.Feature} feature Feature.
* @private
*/
ol.interaction.Snap.prototype.forEachFeatureAdd_ = function(feature) {
this.addFeature(feature);
};
/**
* @param {ol.Feature} feature Feature.
* @private
*/
ol.interaction.Snap.prototype.forEachFeatureRemove_ = function(feature) {
this.removeFeature(feature);
};
/**
* @return {ol.Collection.<ol.Feature>|Array.<ol.Feature>}
* @private
*/
ol.interaction.Snap.prototype.getFeatures_ = function() {
var features;
if (!goog.isNull(this.features_)) {
features = this.features_;
} else if (!goog.isNull(this.source_)) {
features = this.source_.getFeatures();
}
goog.asserts.assert(goog.isDef(features));
return features;
};
/** /**
* Handle 'pointerdown', 'pointermove' and 'pointerup' events. * Handle 'pointerdown', 'pointermove' and 'pointerup' events.
* @param {ol.MapBrowserEvent} evt A move event. * @param {ol.MapBrowserEvent} evt A move event.
@@ -217,8 +241,7 @@ ol.interaction.Snap.prototype.handleFeatureRemove_ = function(evt) {
feature = evt.element; feature = evt.element;
} }
goog.asserts.assertInstanceof(feature, ol.Feature); goog.asserts.assertInstanceof(feature, ol.Feature);
this.removeFeature(feature, this.removeFeature(feature);
feature.getGeometry().getExtent());
}; };
@@ -240,14 +263,13 @@ ol.interaction.Snap.prototype.handleGeometryChanged_ = function(feature, evt) {
/** /**
* @param {ol.Feature} feature Feature * @param {ol.Feature} feature Feature
* @param {ol.Extent} extent Extent.
* @param {boolean=} opt_unlisten Whether to unlisten to the geometry change * @param {boolean=} opt_unlisten Whether to unlisten to the geometry change
* or not. Defaults to `true`. * or not. Defaults to `true`.
* @api * @api
*/ */
ol.interaction.Snap.prototype.removeFeature = function(feature, extent, ol.interaction.Snap.prototype.removeFeature = function(feature, opt_unlisten) {
opt_unlisten) {
var unlisten = goog.isDef(opt_unlisten) ? opt_unlisten : true; var unlisten = goog.isDef(opt_unlisten) ? opt_unlisten : true;
var extent = feature.getGeometry().getExtent();
var rBush = this.rBush_; var rBush = this.rBush_;
var i, nodesToRemove = []; var i, nodesToRemove = [];
rBush.forEachInExtent(extent, function(node) { rBush.forEachInExtent(extent, function(node) {
@@ -279,28 +301,29 @@ goog.exportProperty(
ol.interaction.Snap.prototype.setMap = function(map) { ol.interaction.Snap.prototype.setMap = function(map) {
var currentMap = this.getMap(); var currentMap = this.getMap();
var keys = this.featuresListenerKeys_; var keys = this.featuresListenerKeys_;
var features = this.features_; var features = this.getFeatures_();
var source = this.source_;
if (currentMap) { if (currentMap) {
keys.forEach(ol.Observable.unByKey, this); keys.forEach(ol.Observable.unByKey, this);
keys.clear(); keys.clear();
features.forEach(this.forEachFeatureRemove_, this);
} }
goog.base(this, 'setMap', map); goog.base(this, 'setMap', map);
if (map) { if (map) {
if (!goog.isNull(features)) { if (!goog.isNull(this.features_)) {
keys.push(features.on(ol.CollectionEventType.ADD, keys.push(this.features_.on(ol.CollectionEventType.ADD,
this.handleFeatureAdd_, this)); this.handleFeatureAdd_, this));
keys.push(features.on(ol.CollectionEventType.REMOVE, keys.push(this.features_.on(ol.CollectionEventType.REMOVE,
this.handleFeatureRemove_, this)); this.handleFeatureRemove_, this));
} else if (!goog.isNull(source)) { } else if (!goog.isNull(this.source_)) {
keys.push(source.on(ol.source.VectorEventType.ADDFEATURE, keys.push(this.source_.on(ol.source.VectorEventType.ADDFEATURE,
this.handleFeatureAdd_, this)); this.handleFeatureAdd_, this));
keys.push(source.on(ol.source.VectorEventType.REMOVEFEATURE, keys.push(this.source_.on(ol.source.VectorEventType.REMOVEFEATURE,
this.handleFeatureRemove_, this)); this.handleFeatureRemove_, this));
} }
features.forEach(this.forEachFeatureAdd_, this);
} }
}; };
@@ -367,7 +390,7 @@ ol.interaction.Snap.prototype.snapTo = function(pixel, pixelCoordinate, map) {
* @private * @private
*/ */
ol.interaction.Snap.prototype.updateFeature_ = function(feature) { ol.interaction.Snap.prototype.updateFeature_ = function(feature) {
this.removeFeature(feature, feature.getGeometry().getExtent(), false); this.removeFeature(feature, false);
this.addFeature(feature, false); this.addFeature(feature, false);
}; };