[OpenLayers-Users] Problem with styling vectorfeatures depending on their attributes

Andreas Hocevar ahocevar at opengeo.org
Thu Sep 17 10:10:24 EDT 2009


Max Stephan wrote:
> Hi list,
>
> I have a vectorlayer to which the user can add features and modify those
> features. The user can change the attributes for the features like
> fillColor, strokeColor etc. in a form. A javascript-method is executed by a
> button-click and saves those attributes as vectorattributes (e.g.
> feature.attributes.fillColor).
>
> In the styleMap I´m reading those values from the vectorattributes to change
> the styling. The stylemap-Code looks like this:
> var pointStyleMap = new OpenLayers.StyleMap({
> 	"default": new OpenLayers.Style({
> 	fillColor: "${fillColor}",
> 	fillOpacity: 0.5,
> 	strokeColor: "${strokeColor}",
> 	strokeWidth: "${strokeWidth}",
> 	pointRadius: 10,
> 	graphicZIndex: "${graphicZIndex}"
> 	}
> 	),
> 	"select": new OpenLayers.Style({
> 	pointRadius: 10,
> 	strokeColor: '#FF3333',
> 	strokeWidth: "${strokeWidth}"
> 	}
> 	)
> });This works so far. But now I want to add 2 to the strokeWidth when the
> feature is selected, so I tried it like this:
> strokeWidth: "${strokeWidth}" + 2The effect is that the 2 is only appended
> (the code seems to interprete strokeWidth as a String although I parsed it
> to an Int via parseInt(), e.g. for a strokeWidth of 2 I get 22 as a result).
> It´s possible to solve this problem by defining a context for the style but
> in my opinion that´s a little overkill for such a simple task.
> {context:
> 	{strokeWidth: function (feature){return (feature.attributes.strokeWidth +
> 2)}}
> }Now I want to get the pointRadius from the attributes but no matter which
> method I try, it always ends in the error message: "Line: 625 Column: 408,
> invalid Argument (OpenLayer.js)". Also tried to parse it to an Int again
> directly in the styleMap .. no effect.
> I have to use IE for this project so no further debug information is
> available (I´m also not able to install IE8 with it´s debugging features due
> to limited admin rights at my workstation).
>
> Any idea how I could solve this problem?
>   

As soon as you define a context, feature.attributes will no longer be 
what is available in the template. So you should define your context 
like this:

context: {
    strokeWidth: function(feature){...},
    strokeColor: function(feature){...},
    fillColor: function(feature){...},
    graphicZIndex: function(feature){...},
    pointRadius: function(feature){...}
}

Regards,
Andreas.

> Thx in advance
> Max Stephan
>
>   


-- 
Andreas Hocevar
OpenGeo - http://opengeo.org/
Expert service straight from the developers.




More information about the Users mailing list