OpenLayers eraseFeatures does not erase functions from the map screen

I use OpenLayers and have a layer for my Map and one vector layer. In this vector layer, I use the DrawFeature control to draw a square. I have a listener waiting to add a function and then delete any existing functions (I want only one square at a time), for example:

  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 see that it is always 1, as expected, but the squares on the screen are still showing. Even when I call .redraw () on the layer, the squares still exist.

Is there any extra step that I am missing?

Edit: here you can find my code: http://pastie.org/909644

Edit: I just realized: if I draw a square from the previously existing coordinates, I can clear it just fine. It seems these are just the squares extracted from the controller, which are the problem?

+3
source share
4 answers

you need to create a control first:

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 an active button action to clear the layer of information or functions

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();
      }
  }
}

Hope this serves you sorry for my english

+6
source

, , . , , , , . , , , , .

, WHILE, .

, , "featureadded", , . .

     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
+2

...

    map.layers[1].removeAllFeatures();

1 !

+1

, 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;
0

Source: https://habr.com/ru/post/1740355/


All Articles