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}


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

1 Answer 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.

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.
  • 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
    Commented Jul 15, 2013 at 22:43
  • Do you have an public available page that I can visit?
    – mfdev
    Commented Jul 16, 2013 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
    Commented Jul 17, 2013 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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