[OpenLayers-Users] Style WFS layer by attributes

Jerome Freyre jerome.freyre at hispeed.ch
Tue Sep 1 09:54:32 EDT 2009


Try to apply a stylemap to your vector layer.

// Creation of a stylemap
myStyles = new OpenLayers.StyleMap({
        "default": new OpenLayers.Style({
            strokeColor: "${getFillColor}",
            fillColor: "${getFillColor}",
            strokeWidth: 2,
            strokeOpacity: 1,
            fillOpacity: 0.7,
            pointRadius: 5
        },{
            context: {
                getFillColor:function(feature) {
                    if (feature.attributes.state == true)
                        return '#00FF00';
                    else if (feature.attributes.state == false)
                        return '#FF0000';
                }
            }
        })
        ,"select": new OpenLayers.Style({
            fillColor: "#00000FF",
            strokeColor: "#0000FF",
            pointRadius: 6
        }),
        "hover": new OpenLayers.Style({
            fillColor: "#CCCCCC",
            strokeColor: "#999999",
            pointRadius: 6
        })
    });

// Create a vector layer and give it your style map.
var points = new OpenLayers.Layer.Vector(
                 'Points', {styleMap: myStyles}
             );

if you still have problems, please send you script, i'll try to debug

Jérome


Le 1 sept. 09 à 15:48, AlessioDL (via Nabble) a écrit :

> Hi Jerome,
>
> probably I don't understood exactly how to apply your suggestion...
>
> If I use "new OpenLayers.Style" I can't apply any style to my  
> features.
> The only way to style them appear to be:
> OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default'])
>
> So I need to do something like
>
> //Vector layer style
> var area_style = OpenLayers.Util.extend({},  
> OpenLayers.Feature.Vector.style['default']);
>
> if (feature.attributes.myattribute == 'myvalue') {
>      area_style.strokeWidth = 1.5;
>      area_style.strokeColor = "#ff0000";
>      area_style.fillOpacity = 0.75;
> } else {
> ...
> }
>
> but it does not work (feature is undefined)...
>
> View message @ http://n2.nabble.com/Style-WFS-layer-by-attributes-tp3559456p3560624.html
> To unsubscribe from Re: Style WFS layer by attributes, click here.
>


-- 
View this message in context: http://n2.nabble.com/Style-WFS-layer-by-attributes-tp3559456p3560660.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20090901/4903e107/attachment.html


More information about the Users mailing list