OpenLayers eraseFeatures不会从地图屏幕中删除功能

时间:2010-04-08 17:23:15

标签: openlayers

我正在使用OpenLayers,并为我的Map和一个Vector Layer提供了一个图层。在这个矢量图层中,我使用DrawFeature控件绘制一个正方形。我有一个监听器等待添加一个功能,然后删除任何现有的功能(我一次只想要一个方格),如下所示:

  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

当我检查我的layer.features.size时,我可以看到它总是1,就像我期望的那样,但仍然会显示屏幕上的方块。即使我在图层上调用.redraw(),方块仍然存在。

我缺少一些额外的步骤吗?

修改:您可以在此处找到我的代码:http://pastie.org/909644

编辑:刚刚意识到:如果我从先前存在的坐标中绘制一个正方形,我可以将其清除。它似乎只是从控制器中提取的方块是一个问题?

4 个答案:

答案 0 :(得分:7)

首先需要创建一个控件:

ClearMap.clearMapControl = function () {
    var control = new OpenLayers.Control.Button();
    Map.map.addControl(control);
    return control;
};

然后你需要一个按钮来激活ClearMapControl

ClearMap.newClearMapButton = function () {
    var button = new OpenLayers.Control.Button({
        id : 'clearMapButton',
        type : 1,
        title : 'Clear Map',
        displayClass : 'clearMapButton',
        trigger : clearControlActivate,
    });
    return button;
}

然后您需要激活按钮的操作以清除信息或功能层

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

我希望它能为你服务 对不起我的英文

答案 1 :(得分:2)

好的,我想我已经弄明白了。我意识到,如果他们被定期绘制,我可以清除方块,这让我意识到问题出现在我之前添加过的代码中。我摆脱了那里的方块的清除,果然,当我从一个按钮手动调用清除时,它们就消失了。

因此,在添加功能时尝试清除所有功能是一个问题。

我最终做的是使用“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 :(得分:1)

从矢量图层删除要素后,应将其应用于所有地图...

    map.layers[1].removeAllFeatures();

1是我的矢量图层的参考!

答案 3 :(得分:0)

以下是我们在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;