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];
}
Answer
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.
No comments:
Post a Comment