[OpenLayers-Dev] Feature.style doesn't support contexts

Andreas Hocevar ahocevar at opengeo.org
Mon Jan 18 07:08:10 EST 2010


Hi,

Alexandre Dube wrote:
>   I want to be able to save a OpenLayers.Style object or hash to a 
> vector feature.  Currently, in code, feature.style exists but you must 
> define a hash or else it doesn't work.
>   

That's correct and expected. Let's call your hash "symbolizer".

>   My problem is that I want to be able to draw features to be used as 
> labels (floating text on the map).  To be able to do so, I need to 
> define a StyleMap with a context to display the label (I manually set 
> feature.isLabel on "featureadded") [1]
>   

You can evaluate the symbolizer from the context before setting 
feature.style:

feature.style = myStyleMap.styles["default"].createSymbolizer(feature);

>   That works well, but as soon as I want to "bind / save" the style to 
> the feature, I mean set feature.style, I need to set a hash and the 
> feature will be rendered without the contexts.
>   

The above snippet creates a symbolizer from the default style of the 
style map, and evaluates everything from the context.

>   I don't fully understand how the contexts, symbolizers, etc. work, but 
> my first guess would be to support OpenLayers.Style object directly in 
> the feature object.  Does that make sense ?
>   

No. Context-based styling and symbolizers-per-feature are two different 
things.

>   Or maybe I'm doing it wrong and their's an other way of "labeling" 
> features.  My goal is to create features on a "cosmetic" layer with a 
> default style, then to be able to change the style of the features and 
> bind it to them (I mean without using rules on the layer level, but by 
> defining directly the feature style)
>   

That makes sense, and the snippet above is the solution. You start with 
a layer with a StyleMap, and to override the evaluated style for a 
specific feature, you set feature.style with the symbolizer you get from 
Style::createSymbolizer.

Note that if you want to change the label for such a feature later, you 
need to set symbolizer.label, not feature.attributes.name.

Also note that if you want a point feature with just a label and without 
the geometry rendered, it is better to set the graphic: false symbolizer 
property instead of pointRadius: 0.

Regards,
Andreas.

> Many many thanks,
>
> Alexandre
>
>
> // [1] START
>             var labelStyle = {
>                 label: "${name}",
>                 labelSelect: true,
>                 pointRadius: "${radius}"
>             };
>             var styleOptions = {
>               context: {
>                   'name': function(feature) {
>                       var szValue;
>                       if (feature.attributes['name'] && feature.isLabel 
> === true) {
>                           szValue = feature.attributes['name'];
>                       } else {
>                           szValue = "";
>                       }
>                       return szValue;
>                   },
>                   'radius': function(feature) {
>                       return (feature.isLabel === true) ? 0 : 6;
>                   }
>               }
>             };
>
>             var style = new OpenLayers.Style(
>                 OpenLayers.Util.applyDefaults(
>                     labelStyle,OpenLayers.Feature.Vector.style["default"]),
>                 styleOptions
>             );
>
>             var styleMap = new OpenLayers.StyleMap({
>                 "default": style
>             });
>       
>             layer = new OpenLayers.Layer.Vector(
>                 "Cosmetic", {'styleMap': styleMap});
>
> // [1] END
>
>   


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




More information about the Dev mailing list