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

Alexandre Dube adube at mapgears.com
Fri Jan 15 14:10:13 EST 2010


Hi,

  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.

  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]

  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.

  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 ?

  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)

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

-- 
Alexandre Dubé
Mapgears
www.mapgears.com




More information about the Dev mailing list