I am having a problem with the styling of a WFS vector layer from GeoServer after cloning features from it and adding them to a new feature.
First I create a new empty vector layer and add it to the map object:
var polyAdHoc = new OpenLayers.Layer.Vector("Search Results");
map.addLayer(polyAdHoc);
Then I use a selectFeature control to get a feature from another WFS layer:
var mLayer = map.getLayersBy('name', 'Test Stands 4thQ 2012' );
var name = mLayer[0].name;
var layerFeatureArray = mLayer[0].features;
ctrlSearch.select(layerFeatureArray[x]);
Then I clone the feature from the WFS vector layer:
var cloneFeature = layerFeatureArray[x].clone();
Then I add the cloned feature to the vector layer :
polyAdHoc.addFeatures(cloneFeature);
Then I set the style of the feature in the new vector layer so that it will appear hilighted against the original layer:
cloneFeature.style.strokeColor = '#FF0000';
cloneFeature.style.strokeWidth = 3;
cloneFeature.style.fillOpacity = 0.2;
cloneFeature.style.fillColor = '#FF0000';
The new layer refreshes and the map looks fine until I turn the original WFS layer off and then on again. When it comes on again the features from the WFS that were cloned now share the same style as the cloned features and I want them to stay the same as they were originally.
I do not want the style to apply to the original WFS shape, only the features in the new layer.
Even more frustrating, if I try to reset the original WFS feature style it changes the new vector layers style so I end up with the same problem of both layers having the same style again.
I am using OpenLayers 2.12 and GeoServer 2.1.4.
Any suggestions would be appreciated. Thanks!
@CaptDragon - EDIT:
Here is the complete code I am using now. Everything here works great except for the fact that this changes the style of the features in the original WFS layer.
I have modified this slightly from my original post. Instead of cloning the features I am just doing a straight-up addFeature to the new vector layer.
//FIRST I GET THE WFS LAYER I WANT TO COPY FEATURES FROM
var mLayer = map.getLayersBy('name', 'Test Stands 4thQ 2012' );
/THEN I CREATE A VARIABLE FOR THE ARRAY OF FEATURES IN THE LAYER
var layerFeatureArray = mLayer[0].features; //the array of features in a layer
//THEN I CREATE THE NEW EMPTY VECTOR LAYER
var polyAdHoc = new OpenLayers.Layer.Vector("Search Results");
//I ADD THE NEW EMPTY VECTOR LAYER TO THE MAP OBJECT
map.addLayer(polyAdHoc);
//THE selectItems IS AN ARRAY OF VALUES I RETRIEVE FROM A DATABASE
//I LOOP THROUGH THESE ITEMS
for (a = 0; a < selectItems.length; a++) {
//THEN I LOOP THROUGH THE FEATURES IN THE WFS LAYER
for (x = 0; x < layerFeatureArray.length; x++) {
//IF THE UID FROM THE WFS FEATURE MATCHES THE VALUE FROM THE selectItems VAR
if (layerFeatureArray[x].attributes['UID'] == selectItems[a]) {
//I ASSIGN A VARIABLE TO THE FEATURE
var searchFeature = layerFeatureArray[x];
//AND THEN ADD THE FEATURE TO MY NEW VECTOR LAYER
polyAdHoc.addFeatures(searchFeature);
//AS I ADD EACH FEATURE I GIVE IT STYLING
searchFeature.style.strokeColor = '#FF0000';
searchFeature.style.strokeWidth = 3;
searchFeature.style.fillColor = '#FF0000';
searchFeature.style.fillOpacity = 0.2;
break;
}//END if
} //END feature array FOR LOOP
} //END selectItems FOR
//ZOOM TO THE EXTENT OF THE NEW VECTOR LAYER
map.zoomToExtent(polyAdHoc.getDataExtent());