I answered this same question here today how Robert suggested.
http://gis.stackexchange.com/questions/20315/how-to-parse-polygons-from-gml-
in-openlayers

all polygons in the same layer get a random color:
http://jsfiddle.net/YrpEY/1/


- Carlos 


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Robert Sanson
Sent: Wednesday, February 01, 2012 3:13 PM
To: [email protected]; Peter
Subject: [OpenLayers-Users] Re: drawing polygons with different Colors

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) {
      alert(response.responseText);
    }
    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";
          crops.addFeatures(cropgeom);
                cropnum = cropnum + 1;
        }
      }
      map.addLayer(crops);
}

regards,

Robert Sanson

>>> Peter <[email protected]> 1/02/2012 9:14 p.m. >>>
So nobody have any ideas?

Greetings

--
View this message in context:
http://osgeo-org.1560.n6.nabble.com/drawing-polygons-with-different-Colors-t
p3910758p4355559.html 
Sent from the OpenLayers Users mailing list archive at Nabble.com.
_______________________________________________
Users mailing list
[email protected] 
http://lists.osgeo.org/mailman/listinfo/openlayers-users



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.
www.websense.com
_______________________________________________
Users mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/openlayers-users

_______________________________________________
Users mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/openlayers-users

Reply via email to