OpenLayers eraseFeatures doesn't erase features from map screen
I'm using OpenLayers, and have a layer for my Map, and a single Vector Layer. In this vector layer, I am using the DrawFeature control to draw a square. I have a listener waiting for a feature to be added, and then deleting any existing features (I only want one square at a time), like so:
polygonLayer.events.register("beforefeatureadded", feature, function(evt){
console.log("Clearing existing polygons");
console.log(polygonLayer.features.length);
polygonLayer.destroyFeatures();
polygonLayer.redraw();
});//end attempt at events registration
When I check my layer.features.size, I can see that it's always 1, just like I expect, but the squares on the screen are still displayed. Even when I call .redraw() on the 开发者_JAVA技巧layer, the squares are still there.
Is there some extra step I'm missing?
Edit: You can find my code here: http://pastie.org/909644
Edit: Just realized: If I draw a square from previously existing coordinates, I can clear it just fine. It seems to be just the squares drawn from the controller that are an issue?
you need first to create a control:
ClearMap.clearMapControl = function () {
var control = new OpenLayers.Control.Button();
Map.map.addControl(control);
return control;
};
then you need a button to activate ClearMapControl
ClearMap.newClearMapButton = function () {
var button = new OpenLayers.Control.Button({
id : 'clearMapButton',
type : 1,
title : 'Clear Map',
displayClass : 'clearMapButton',
trigger : clearControlActivate,
});
return button;
}
then you need the action of the button active for clear the layer of infos or features
var clearControlActivate = function(){
for(var i=0; map.popups.length; i++) {
map.removePopup(map.popups[i]);
}
for(var j=0; j < map.layers.length; j++){
if(map.layers[j].__proto__.CLASS_NAME == "OpenLayers.Layer.Vector"){
map.layers[j].removeAllFeatures();
}
}
}
i hope it serve to you sorry for my english
Okay, I think I have this figured out. I realized that I could clear the squares if they were drawn regularly, and that made me realize that the problem was in my beforefeatureadded code. I got rid of the clearing of the squares there, and sure enough, when I called the clear manually from a button, they went away.
So, trying to clear all features WHILE adding a feature was a problem.
What I ended up doing is using "featureadded" and then removing the feature from the list of features to be removed. Works like a charm.
polygonLayer.events.register("featureadded", feature, function(evt){
//only one circle at a time
points = evt.feature.geometry.getVertices();
console.log("Erasing old features: ");
console.log(evt.feature);
if(polygonLayer.features.length > 1){
polygonLayer.destroyFeatures(polygonLayer.features.shift());
};
});//end after feature
After deleting features from the vector layer, you should apply it to all the map ...
map.layers[1].removeAllFeatures();
1 is the reference of my vector layer !
Here's the code we're using in Legato:
Legato.Control.DrawFeature = OpenLayers.Class(OpenLayers.Control.DrawFeature, {
mode :1,
callback :null,
handlerConstructor :OpenLayers.Handler.Point,
initialize : function(layer, options) {
OpenLayers.Control.DrawFeature.prototype.initialize.apply(this, [ layer,
this.handlerConstructor, options ]);
},
destroy : function() {
OpenLayers.Control.DrawFeature.prototype.destroy.apply(this, arguments);
},
setMap : function(map) {
OpenLayers.Control.DrawFeature.prototype.setMap.apply(this, arguments);
},
drawFeature : function(geometry) {
var feature = new OpenLayers.Feature.Vector(geometry);
var proceed = this.layer.events.triggerEvent("sketchcomplete", {
feature :feature
});
if (proceed !== false) {
feature.state = OpenLayers.State.INSERT;
if (this.mode == 1) {
this.layer.addFeatures( [ feature ]);
this.featureAdded(feature);
this.events.triggerEvent("featureadded", {
feature :feature
});
}
if (this.mode == 2) {
this.layer.destroyFeatures();
this.layer.addFeatures( [ feature ]);
this.featureAdded(feature);
this.events.triggerEvent("featureadded", {
feature :feature
});
}
}
if (Legato.Lang.ObjectUtils.isFunction(this.callback)) {
this.callback(geometry);
}
},
CLASS_NAME :'Legato.Control.DrawFeature'
});
Legato.Control.DrawFeature.MODE_ADD_FEATURE = 1;
Legato.Control.DrawFeature.MODE_REPLACE_FEATURE = 2;
精彩评论