[OpenLayers-Users] Re: drawing polygons with different Colors

Robert Sanson Robert.Sanson at asurequality.com
Wed Feb 1 15:13:19 EST 2012

If you are bringing in vector features from a JSON / WKT / GML source, then you can assign an attribute with the fill color, then assign a style to the layer, with fill color using the attribute as a variable:

var my_style = new OpenLayers.StyleMap({
    "default": new OpenLayers.Style(
            pointRadius: 3,
            strokeColor: "#ff6103",
            fillColor: "${fillcolor}",
            fillOpacity: 0.4,
            strokeWidth: 2,
            //label: "${name}",
            fontColor: "blue",
            fontSize: 10,
            fontFamily: "Arial",
            fontWeight: "bold"

function setHTML(response) {
    var cropnum = 0;
    var lines, line;
    var cropid, name, fcolor, crds;
    var cropgeom;
    if (response.responseText.toUpperCase().indexOf('ERROR') != -1) {
    else {
      crops = new OpenLayers.Layer.Vector("Crops",
                  {isBaseLayer: false,
                   styleMap: my_style,
                   rendererOptions: {zIndexing: true},
                   extractAttributes: true 
      var lines = response.responseText.split('\n');
      for ( var i=0;i<lines.length;i++) {
        line = lines[i].split("|");
        if ( line.length == 3 ) {
          cropid = line[0];
          name = line[1];
         fcolor = line[2];
          crds = line[3];
          cropgeom = wkt_format.read(crds);
          cropgeom.attributes.id = cropid;
          cropgeom.attributes.name = name;
          cropgeom.attributes.fcolor = fcolor;
          cropgeom.attributes.pstate = "pre";
		cropnum = cropnum + 1;


Robert Sanson

>>> Peter <pfischer at proveo.com> 1/02/2012 9:14 p.m. >>>
So nobody have any ideas?


View this message in context: http://osgeo-org.1560.n6.nabble.com/drawing-polygons-with-different-Colors-tp3910758p4355559.html 
Sent from the OpenLayers Users mailing list archive at Nabble.com.
Users mailing list
Users at lists.osgeo.org 

This email and any attachments are confidential and intended solely for the addressee(s). If you are not the intended recipient, please notify us immediately and then delete this email from your system.

This message has been scanned for Malware and Viruses by Websense Hosted Security.

More information about the Users mailing list