Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ="POLYGON((" + polyString + "))");

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

share|improve this question
up vote 2 down vote accepted

try this:

var style = OpenLayers.Util.extend({},['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


By posting your answer, you agree to the privacy policy and terms of service.

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