If you want to change a style depending on a property of the layer, you can do this by supplying a function as a style context.
Here's an example where the colours were stored in a hash array:
var jobContext = {
getColour: function (feature, styleEl)
{
var jobSector = feature.attributes[<myProperty>];
return sectorColours[jobSector];
}
};
To use it, pass in the object, in this case jobContext, to your style:
var ocSketchSymbolizersGeom = {
"Point": {
pointRadius: 4,
graphicName: "circle",
fillColor: "${getColour}",
fillOpacity: 1,
strokeWidth: 1,
strokeOpacity: 1,
strokeColor: "${getColour}",
graphicZIndex: 1
},
"Line": {
strokeWidth: 6,
strokeOpacity: 1,
strokeColor: "${getColour}",
graphicZIndex: 1
},
"Polygon": {
strokeWidth: 2,
strokeOpacity: 1,
strokeColor: "${getColour}",
fillColor: "${getColour}",
fillOpacity: 0.7,
graphicZIndex: 1
}
};
var ocStylesGeom = new OpenLayers.Style(null, { context: jobContext });
ocStylesGeom.addRules([new OpenLayers.Rule({ symbolizer: ocSketchSymbolizersGeom })]);
var markerStyleMap = new OpenLayers.StyleMap(
{ 'default': ocStylesGeom,
'select': ocStylesGeom
},
{ extendDefault: true }
);
I can't find examples in the OpenLayers site, but there are more examples on various web sites.