如何在不丢失现有选择的情况下将图层添加到SelectFeature?


9

我正在使用OpenLayers.Control.SelectFeature进行多层选择。但是,当我使用setLayer()添加图层时,其他图层上的选择都会丢失。

有人知道如何解决此问题吗?当我向SelectFeature控件添加图层时,我希望将现有选择保留在其他图层上。

这是一个例子: 我的例子

更新:

我知道这是API的一部分。但是我正在寻找解决方法。

/**
 * APIMethod: setLayer
 * Attach a new layer to the control, overriding any existing layers.
 *
 * Parameters:
 * layers - Array of {<OpenLayers.Layer.Vector>} or a single
 *     {<OpenLayers.Layer.Vector>}
 */
setLayer: function(layers) {
    var isActive = this.active;
    this.unselectAll();
    this.deactivate();
    if(this.layers) {
        this.layer.destroy();
        this.layers = null;
    }
    this.initLayer(layers);
    this.handlers.feature.layer = this.layer;
    if (isActive) {
        this.activate();
    }
},

Answers:


6

您可以修改SelectFeature控件的setLayer方法:

OpenLayers.Control.SelectFeature.prototype.setLayer = function(layers) {
    var isActive = this.active;
    //this.unselectAll(); <- what you need
    this.deactivate();
    if(this.layers) {
        this.layer.destroy();
        this.layers = null;
    }
    this.initLayer(layers);
    this.handlers.feature.layer = this.layer;
    if (isActive) {
        this.activate();
    }
}

当然,我可以!不知道为什么我没有想到这一点。谢谢!(+1)
CaptDragon 2011年

0

我认为这是一种非常有用的方法,但应将其添加为addLayer(Layer)并进行一些小的更改以使其处理一层:

addLayer = function(layer) {
  var isActive = this.active;
  var currLayers = this.layers; 
  this.deactivate();
  if(this.layers) {
      this.layer.destroy();
      this.layers = null;
  }
  if ( currLayers != null) {
      currLayers.push(layer);   
      this.initLayer(currLayers);
  } else {
      this.initLayer([layer]);
  }
  this.handlers.feature.layer = this.layer;
  if (isActive) {
      this.activate();
  }
},

我希望它将在此请求中接受-https://github.com/openlayers/openlayers/pull/1287

同样,用户也不必维护已添加的图层列表。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.