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.

When I zoom or move the map my layer is redrawn. This is what I want, because I have allot of features to load at once. This is the code:

    wfs = new OpenLayers.Layer.Vector("WFS", {
    strategies: [new OpenLayers.Strategy.BBOX({
                    resFactor: 1,
                    ratio:1
                })],
    protocol: new OpenLayers.Protocol.WFS({
              maxFeatures:1000,
              url:  myUrl,
              featureType: myFeatureType,
              featureNS: myFeaturedNS,
              version: "1.1.0"
              }),
      filter: setFilter(year, variant)
});

The following code is my selectioncode. This all works as I wan't it to. However, when the layer is redrawn the selection is lost and the onFeatureUnselect is not fired (which it should). I'm not sure if the selection is really lost or that the style is resetted.

    selectCtrl = new OpenLayers.Control.SelectFeature(
        wfs,
        {
            clickout: true, toggle: false,
            multiple: false, hover: false,
            box: true,
            toggleKey: "shiftKey", // ctrl key removes from selection
            multipleKey: "shiftKey",
            eventListeners: {
                featurehighlighted: onFeatureSelect,
                featureunhighlighted: onFeatureUnselect

            }
        }
    );

@geographika

My onFeatureSelect and onFeatureUnselect functions:

function onFeatureSelect(e) {
var feature = e.feature.attributes;
var featureclone = e.feature.clone();

var style = {
    pointRadius: 10,
    fillOpacity:0,
    strokeColor: "#000000"
    };

featureclone.style = style;

layer2.addFeatures([featureclone]);
selectedFeatures[featureclone.attributes.receptor_id] = featureclone;
}

function onFeatureUnselect(e) {
    var feature = e.feature.attributes;
    layer2.removeFeatures(selectedFeatures[feature.receptor_id]);   
    delete selectedFeatures[feature.receptor_id];
}
share|improve this question
    
What is in your onFeatureSelect function? –  geographika Oct 21 '11 at 11:04
    
I've updated my post with the two functions –  Boedy Oct 21 '11 at 12:22

2 Answers 2

up vote 4 down vote accepted

I've found the easiest way to maintain vector selections across zooming, panning, and redraws is to add a new empty vector layer to your map, and when features are selected place a copy in this layer.

You can set the select layer style to be an clearly visible style.

In your select tool highlighting functions, add a clone of the feature when selecting, and remove it when it is selected a second time.

onFeatureSelect(feat){
  //check if the feature is in the select layer
  var cloned_feat = selectionLayer.getFeatureById(feat.id); 
  //or a getFeatureBy with a unique property

  if(cloned_feat){
      selectionLayer.removeFeatures([cloned_feat]);
  }
  else {
      var featCopy = feat.clone();
      this.sketchLayer.addFeatures([featCopy]);
  }
}

This is slightly simplified (and untested) - you will probably want to store the selection layer as a property of your select tool.

Also check the getFeatureById function - you may have to use a different unique property on your feature rather than the ID.

share|improve this answer
    
Thanks, for the reply. I think this might be a good solution. I'm fiddeling with some code, but I'm stuck at the point that the onFeatureUnselect event does not fire when the selection is lost. So on what event do I remove the feature clones? See my original post. –  Boedy Oct 21 '11 at 13:38
    
@Boedy see updated answer. You should probably do everything in the select function. Select once to select, and again to deselect. –  geographika Oct 21 '11 at 14:30
    
Many thanks! Cheching if the feature is in the selected layer helped. Now I just have a styling problem, but I bet I can figure that one out myself. –  Boedy Oct 24 '11 at 11:56

From my experiments (http://ian01.geog.psu.edu/geoserver/www/wfs/wfs-sld2.html) it seems that selection and style should be maintained over a zoom.

share|improve this answer
    
In your case it works. But your code doens't use: resFactor: 1 and ratio:1. which causes the redraw when zoom is changed. Your example doesn't trigger a redraw. –  Boedy Oct 21 '11 at 10:25
    
well that narrows the problem down for you then –  iant Oct 21 '11 at 10:50

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.