如何在飞行中应用OpenLayers Stymbolizer上下文?

时间:2013-06-18 15:46:22

标签: openlayers

由于How to keep OpenLayers.StyleMap and OpenLayers.SelectFeature from conflicting?中详述的原因,我有一些样式代码如下:

myStyle.Events = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
myStyle.Events.extendDefault = true;
myStyle.Events.maxScaleDenominator = 200000000;
myStyle.Events.graphicWidth = 36;
myStyle.Events.graphicHeight = 36;
myStyle.Events.graphicOpacity = 0.75;
myStyle.Events.externalGraphic = "/img/icons/${icon}.png";
myStyle.Events.label = "${count}";
myStyle.Events.labelOutlineWidth = 1;
myStyle.Events.labelOutlineColor = "#000";
myStyle.Events.fontColor = "#000";
myStyle.Events.fontOpacity = 8.0;
myStyle.Events.fontSize = "11px";
myStyle.Events.labelYOffset = 3;

myStyle.Events.context = {
  icon: function(feature) {
    var iconMap,
        iconPath,
        type = "single";

    iconMap = {
      ...
    }

    if(feature.attributes.count) {
      type = "clustered";
    }

    return iconMap[feature.attributes.name][type];
  },
  count: function(feature) {
    var labelText = "";

    if(feature.attributes.count) {
      labelText = feature.attributes.count;
    }

    return labelText;
  }
};

我怎样才能正确地应用那个上下文?我不能做典型的OpenLayers.Style({},{context:context});语法在这里。

1 个答案:

答案 0 :(得分:3)

您必须将context属性应用于OpenLayers.Style实例,如下所示:

var style = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style["default"]);
style.pointRadius = "${radius}";
style.fillColor = '${colorFunction}';

var defaultStyle = new OpenLayers.Style(style, {
    context: {
        colorFunction: function(feature) {
            return colors[feature.attributes.temp];
        }
    }
});

我建议您使用OpenLayers Cookbook(主题7样式)或查看源代码示例http://acanimal.github.io/Openlayers-Cookbook/(第7章 - 使用StyleMap改进样式和功能替换属性)

干杯。