slevin
slevin

Reputation: 3888

set different style for lines in vector openlayers layer

So, I use openlayers 3.9.0 and I have a vector layer that contains LineStrings, Polygons and Points.

By default LineStrings look like black lines, with litte stroke. I try to make them a bit thicker and their color have to be visible, so they not blend in, when many features are rendered.

This is my style

var stroke = new ol.style.Stroke({
        color: 'rgba(0,0,0,0.4)',
        lineCap: "butt",
        lineJoin: "bevel",
        width:1         
    });


function styleFunction(feature, resolution) {
    var color = feature.get('color');
    var name = feature.get('name');
    var geom = feature.getGeometry().getType();

    var fill = new ol.style.Fill({
        color: color
    });

//this does not work    
    if (geom == 'LineString') {
        stroke.width=42;
    }

    var circle = new ol.style.Circle({
        radius: 6,
        fill: fill,
        stroke: stroke,
        color:color
    });

    var text= new ol.style.Text({
            font: '20px Verdana',
            text: name,
            fill: new ol.style.Fill({
                color: [64, 64, 64, 0.75]
            })
        })

    var cStyle = new ol.style.Style({
        fill: fill,
        stroke: stroke,
        image : circle,
        text : text
    });

    return [cStyle];
}

I can not make this work. Any tips?

Thanks

Upvotes: 0

Views: 1296

Answers (1)

Alvin Lindstam
Alvin Lindstam

Reputation: 3142

OpenLayers 3 styles are immutable, they can not be changed.

Instead of trying to modify the width of the Stroke, create a new one with the options you want.

Upvotes: 1

Related Questions