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,
    protocol: new OpenLayers.Protocol.WFS({
              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(
            clickout: true, toggle: false,
            multiple: false, hover: false,
            box: true,
            toggleKey: "shiftKey", // ctrl key removes from selection
            multipleKey: "shiftKey",
            eventListeners: {
                featurehighlighted: onFeatureSelect,
                featureunhighlighted: onFeatureUnselect



My onFeatureSelect and onFeatureUnselect functions:

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

var style = {
    pointRadius: 10,
    strokeColor: "#000000"

featureclone.style = style;

selectedFeatures[featureclone.attributes.receptor_id] = featureclone;

function onFeatureUnselect(e) {
    var feature = e.feature.attributes;
    delete selectedFeatures[feature.receptor_id];
  • 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

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.

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

  else {
      var featCopy = feat.clone();

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.

  • 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.

  • 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 – Ian Turton Oct 21 '11 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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