[OpenLayers-Users] Style WFS layer by attributes

Jerome Freyre jerome.freyre at hispeed.ch
Tue Sep 1 06:39:19 EDT 2009


Hi Alessio,

You can define a style and, depending a context, assign different value.

Here is an example that change the fillcolor of feature depending on the
attribute "state" of each feature.

Note that the context is automatically applied to features. Nothing to
activate ;)

      new OpenLayers.Style({
           strokeColor: "${getFillColor}",
           fillColor: "${getFillColor}",
           strokeWidth: 2,
           strokeOpacity: 1,
           fillOpacity: 0.7,
           pointRadius: 5
       },{
           context: {
               getFillColor:function(feature) {
                   // Green if state is true
                   if (feature.attributes.state == true)
                       return '#00FF00';
                   // Red if state is false
                   else if (feature.attributes.state == false)
                       return '#FF0000';
               }
           }    
       })



AlessioDL wrote:
> 
> Hi, 
> 
> I have this WFS layer from my UMN-Mapserver
> 
> //Vector layer style
> var area_style = OpenLayers.Util.extend({},
> OpenLayers.Feature.Vector.style['default']);
>      area_style.strokeWidth = 1.5;
>     area_style.strokeColor = "#ff0000";
>     area_style.fillOpacity = 0.75;
> 
> 
> //Layer definition 
> qAreas = new OpenLayers.Layer.Vector("WFS",
>     		{
>     	        strategies: [new OpenLayers.Strategy.BBOX()],
>     	        filter: propertyFilter, 
>     	        style:area_style,
>     	        protocol: new OpenLayers.Protocol.WFS({
>     		        url:  wfsURL, 
>     		        extractAttributes:true,
>     		        featureType: "QueryAreas",
>     		        geometryName:"the_geom"
>     	        })
> });
> 
> 
> I'd like to use an attribute value to style things differently 
> (for example, if the value of the attribute named type is "A" the
> corresponding polygons bacame red and if it is "B" they became blue)
> 
> Can you help me? :-)
> 
> 

-- 
View this message in context: http://n2.nabble.com/Style-WFS-layer-by-attributes-tp3559456p3559667.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.



More information about the Users mailing list