Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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();
    }
},
share|improve this question
add comment

2 Answers 2

up vote 6 down vote accepted

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();
    }
}
share|improve this answer
    
Of course i can! Don't know why i didn't think of that. Thanks! (+1) –  CaptDragon Nov 29 '11 at 13:52
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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