0

i need to change the property of some style after its created, and i need to change it with knowing only the string of the property name. i am getting string like "fillColor" and value like "blue", and i need to change the style object. how can i do it? i didnt find anything in the api. the style code:

            var selectStyle = new OpenLayers.Style({
            externalGraphic: 'styles/tree_selected.png',
            graphicWidth: 16, graphicHeight: 26, graphicYOffset: -24,
            title: 'tree'
        });
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.