2

I'm trying to alter the colour of displayed points using addUniqueValueRules in OpenLayers. If I don't use addUniqueValueRules all my points display the default colour (green). However, when I add addUniqueValueRules, only those points that meet the rules are drawn.

The relevant code:

var styleMap = new OpenLayers.StyleMap({
    fillOpacity: 1,
    fillColor: "green",
    pointRadius: 5
});

var styleLookup = {
    "Hög": { fillColor: "red" },
    "Måttlig": { fillColor: "yellow" },
    "Undersöka": { fillColor: "cyan" }
};

styleMap.addUniqueValueRules( "default", "risk", styleLookup );

wfs = new OpenLayers.Layer.Vector("Editable Features", {
    strategies : [new OpenLayers.Strategy.Fixed(), saveStrategy],
    projection: new OpenLayers.Projection("EPSG:4326"),
    protocol: new OpenLayers.Protocol.WFS({
        version: "1.1.0",
        srsName: "EPSG:4326",
        url: "http://localhost/cgi-bin/tinyows",
        featurePrefix: "tows",
        featureNS :  "http://www.tinyows.org/",
        featureType: "tradcopy",
        geometryName: "geom"
    }),
    styleMap: styleMap
}); 

Can anyone help me draw all the points - both default and the ones that meet the rules?

1 Answer 1

2

The addUniqueValueRules implies that features that do not match the lookup object won't be rendered. I don't know if this is by design but the responsible code is here. If your Style have rules (which is not the case when you don't use addUniqueValueRules) and none of them were matched, then the feature is hidden.

This is the workaround I came up with:

var lookup = {
    "OpenLayers.Feature.Vector_1329": {
        fillColor: '#FF0000'
    }
};

this.drawingLayer.styleMap.addUniqueValueRules('default','name',lookup);
this.drawingLayer.styleMap.styles.default.rules.push(new OpenLayers.Rule({
    elseFilter: true,
    symbolizer: this.drawingLayer.styleMap.styles.default.defaultStyle
}));

What it does is simply defining an else rule that takes the default style as a symbolizer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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