2

For the reasons detailed in How to keep OpenLayers.StyleMap and OpenLayers.SelectFeature from conflicting?, I have some style code that looks like:

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;
  }
};

How can I correctly apply that context on the fly like that? I can't do the typical OpenLayers.Style({}, {context: context}); syntax here.

4

1 回答 1

2

您必须将该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 和特性的属性替换改进样式)

干杯。

于 2013-06-18T18:10:30.797 回答