Take the 2-minute tour ×

Still learning OpenLayers and I think my problem may be a lack of understanding the terminology, however.

Is it possible to draw polygons in a different colour within one vector layer, or do all polygons get their colour from the colour set within the layer?

I ask because I have a map which draws the borders of several (up to 10) different countries around the world and I'd rather have one layer with different coloured polygons rather than have several different layers.

I have the following code:

var polyCountry = format.read("POLYGON((" + polyString + "))");

Which works perfectly, but can't see any documentation anywhere on how to style the polygon.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

try this:

var style = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
style.fillColor = '#cc33ff';
style.fillOpacity = 0.5;
style.strokeColor = '#cc33ff';
style.strokeWidth = 1;

var points = [new OpenLayers.Geometry.Point(0, 10),
              new OpenLayers.Geometry.Point(10, 20),
              new OpenLayers.Geometry.Point(20, 30),
              new OpenLayers.Geometry.Point(30, 0)];

var rings = new OpenLayers.Geometry.LinearRing(points);
var polygon = new OpenLayers.Geometry.Polygon([rings]);

var feat = new OpenLayers.Feature.Vector(polygon, null, style);


vector.features[0].style.fillColor = '#cc33ff';

i hope it helps you...

share|improve this answer
Aragon, that does indeed make perfect sense, thank you, however I'm having a difficult time actually getting to work within my code but I will carry on and try to use it. Again, my thanks. –  JasonMHirst Nov 6 '12 at 8:45
edit Got it working after using the "Points" -> "LinearRings" method you mentioned, works perfectly! Again, many thanks. –  JasonMHirst Nov 6 '12 at 11:27

Your Answer


