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

Andreas Hocevar ahocevar at opengeo.org
Thu Sep 17 11:46:39 EDT 2009


Hey Paul,

I had totally forgotten about this ticket. I combined the 2nd patch with 
the test from the 1st patch and uploaded a new patch that you can commit.

Thanks for that contribution, it will make styling easier for sure.

Regards,
Andreas.

Paul Spencer wrote:
> http://trac.openlayers.org/ticket/2146 may help, I filed this a while  
> back with a patch to make it possible to use a context and attribute  
> map.  This lets you use ${} for feature styling from attributes and  
> methods in a context:{} at the same time, reducing the perceived  
> overhead of adding a function for every attribute you want to map.  At  
> the same time, the patch also modifies what is passed to a context  
> function to include the name of the attribute being calculated, which  
> means you can use a single function to handle multiple attributes.
>
> This means you can do something like this contrived example:
>
> new StyleMap({
>    default: {
>      fillColor: "${fillColor}", // should come from feature.attributes
>      strokeWidth: "${getStyle}", // should come from getStyle  
> function, +2 on attribute value
>      strokeColor: "${getStyle}" // should come from getStyle function,  
> just strokeColor attribute though.
>    },
>    context: {
>      getStyle: function(feature, attribute) {
>        if (attribute == 'strokeWidth') {
>          return feature.attributes.strokeWidth + 2;
>        } else {
>          return feature.attributes[attribute];
>        }
>    }
> });
>
> Not sure if this is useful for you.
>
> Cheers
>
> Paul
>
>
> On 2009-09-17, at 10:19 AM, Max Stephan wrote:
>
>   
>> 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.
>> _______________________________________________
>> Users mailing list
>> Users at openlayers.org
>> http://openlayers.org/mailman/listinfo/users
>>     
>
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
>   


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




More information about the Users mailing list