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

Max Stephan x.zam at gmx.net
Thu Sep 17 10:19:39 EDT 2009


Hi Andreas,

Thank you for that hint. I already found that out to but it doesn´t help me
with solving my problem. I prefer to avoid using the context because of
this. Any other ideas on this?

greets
Max Stephan

Andreas Hocevar-2 wrote:
> 
> 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.
> 
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
> 
> 

-- 
View this message in context: http://n2.nabble.com/Problem-with-styling-vectorfeatures-depending-on-their-attributes-tp3663197p3663407.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.



More information about the Users mailing list