[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) {
      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 <pfischer at proveo.com> 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-tp3910758p4355559.html 
Sent from the OpenLayers Users mailing list archive at Nabble.com.
_______________________________________________
Users mailing list
Users at lists.osgeo.org 
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


More information about the Users mailing list