0

I have created two Openlayers vector layers, named "temp" and "drawings1". Each of the layer have different styles. The problem is when I copy a features from drawings1 to temp, the style of "drawings1" is also copied to the "temp" layer. It seems that the pre-configured style of the temp layer is ignored. Is there anyway to reset the style of a vector layer?

//Define a style for the drawings1 layer and temp layer 
var highlight_style = { fillColor:'blue', strokeColor:'#2E2EFE', fillOpacity:0.6 };
var hover_style = { fillColor:'#FF0000', strokeColor:'#FF0000', fillOpacity:0.6 };

//Initialize a vector layer for storing selection Objects
drawings1 = new OpenLayers.Layer.Vector("Selections", {isBaseLayer:false, features:[], visibility:true, style:highlight_style}
        );

//Initialize a vector layer for storing selection Objects
temp = new OpenLayers.Layer.Vector("Hover", {isBaseLayer:false, features:[], visibility:true, style:hover_style}
        );

map.addLayers([drawings1,temp])

//Copy a feature from drawings1 to temp
tempFeature = drawings1.features[0];
temp.addFeatures(tempFeature);
1

That's not the problem of the layer styles, but the matter of javascript object reference assignment. By doing this:

tempFeature = drawings1.features[0];  // Here you are not copying the feature, but referencing the same feature with a new variable name.
temp.addFeatures(tempFeature);

You are not actually copying an feature, but referencing the same feature with a different variable name (tempFeature), which means you are adding the same feature to two layers. And that will cause some style messes.

A simple fix is to use the clone method coming with the feature object (almost all openlayers objects have this method).

tempFeature = drawings1.features[0].clone(); // Clone the feature which will create a new object.
temp.addFeatures(tempFeature);
3
  • Many Thanks.Now a new features is created on the temp layer. However, it is still applying the style of the drawings1 layer. How do I reset style?
    – K Hsueh
    Jul 15 '13 at 22:43
  • Do you have an public available page that I can visit?
    – mfdev
    Jul 16 '13 at 5:40
  • Sorry the application is web based but for internal use. There is no public web page for this web application. I think I am having the same problem mentioned in this post gis.stackexchange.com/questions/46730/…. I am going to try the solutions suggested in this post.
    – K Hsueh
    Jul 17 '13 at 12:48

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.