I have a polygon dataset that I am symbolizing in OpenLayers using a context to determine the correct color, and also a label for each polygon that I want to alter depending on the zoom level.
But although my polygons get colored as they should, my labels don't change. If I test this with a point feature and no symbolization, my labels do change the way I want. So I guess this is something to do with the way rules and context are applied? How can I fix this so that the labels change size with the rules?
var ratingStyle = new OpenLayers.StyleMap({
"default": new OpenLayers.Style({
fillColor: "${getColor}",
strokeColor: "#000000",
fillOpacity: 0.7,
graphicZIndex: 1,
label: "${ADMINUNIT}",
labelAlign: 'cm',
labelOutlineColor: "white",
labelOutlineWidth: 3,
fontSize: 12,
fontOpacity: 1
}, {
context: { //this is for the polygon color
getColor: function (feature) {
return feature.attributes.RATING == 'NotRated' ? 'transparent' :
feature.attributes.RATING == 'Low' ? 'green' :
feature.attributes.RATING == 'Moderate' ? 'yellow' :
feature.attributes.RATING == 'High' ? 'red' :
feature.attributes.RATING == 'Extreme' ? 'black' :
'grey';
}
}
}, { //rules for labels?
rules: [
new OpenLayers.Rule({
minScaleDenominator: 50000000,
symbolizer: {
fontSize: "0px"
}
}),
new OpenLayers.Rule({
maxScaleDenominator: 50000000,
minScaleDenominator: 20000000,
symbolizer: {
fontSize: "10px"
}
}),
new OpenLayers.Rule({
maxScaleDenominator: 20000000,
symbolizer: {
fontSize: "12px"
}
})
]
}),
"select": new OpenLayers.Style({
fillOpacity: 1,
graphicZIndex: 1
})
}, {
extendDefault: true
});
Best Answer
Have you tried to realize the scale-dependent font size with a second context-function? You can get the scale with map.getScale()