How to draw lines with different colors in Openlayers 2?

Every feature has a style property which is null by default because it inherits the parent vector layer's style. But you can set the style for each feature:

DEMO

enter image description here

DEMO LINK

Code Example:

var myFirstLineStyle = OpenLayers.Util.applyDefaults(myFirstLineStyle, OpenLayers.Feature.Vector.style['default']);
myFirstLineStyle.strokeColor = "#ffffff";
myFirstLineStyle.strokeWidth = 8;
firstFeature.style = myFirstLineStyle;

var mySecondLineStyle = OpenLayers.Util.applyDefaults(mySecondLineStyle, OpenLayers.Feature.Vector.style['default']);
mySecondLineStyle.strokeColor = "#000000";
mySecondLineStyle.strokeWidth = 4;
secondFeature.style = mySecondLineStyle;

If you change the styles of the features after they've been added to the layer you must call layer.redraw().

More on Styling

Tags:

Openlayers 2