9

I'm using an OpenLayers.Control.SelectFeature for selections on multiple layers. However, when I add a layer using setLayer(), my selections on the other layers are lost.

Anyone know how to work around this? I would like to keep my existing selections on other layers when I add a layer to the SelectFeature control.

Here is an example: MY EXAMPLE

Update:

I'm aware that this is part of the API. But i'm looking for a work around.

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

2 Answers 2

6

You could modify setLayer method of SelectFeature control:

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
  • Of course i can! Don't know why i didn't think of that. Thanks! (+1)
    – CaptDragon
    Commented Nov 29, 2011 at 13:52
0

I think this is a very useful method but it should be added as addLayer(Layer) and with some small changes to make it handle one 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();
  }
},

which i hope will be taken in in this pull request - https://github.com/openlayers/openlayers/pull/1287

Also then the user dont have to maintain a list of layers that have been added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.