1

I use the following peace of code to show multiple LineStrings which have a point as the last one, along with a label:

var strokeColors = [...];//"#FFAACC" kind-of elements
var linesLayer = new OpenLayers.Layer.Vector("Lines Layer", {
    styleMap: new OpenLayers.StyleMap({'default':{
        pointRadius: 6,
        labelYOffset: -15,
        labelOutlineColor: "white",
        labelOutlineWidth: 3,
        label: "${label}"
    }})
});
var feats = [];
for(var j=0;j<lines.length;j++){
    var points = [];
    var feat;
    for(var i=0;i<lines[j].length;i++){
        var p = new OpenLayers.Geometry.Point(lines[j][i].x,lines[j][i].y);
        if(i==lines[j].length-1){
            feat = p;
        }
        points.push(p);
    }
    var lineString = new OpenLayers.Geometry.LineString(points);
    var lineFeature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Collection([lineString, feat]));
    lineFeature.style = {
        strokeColor: strokeColors[j%4],
        strokeOpacity: 1.0,
        strokeWidth: 6
    };
    lineFeature.attributes = {
        label: lines[j].lab
    };
    feats.push(lineFeature);
}
linesLayer.addFeatures(feats);
map.addLayer(linesLayer);

This shows the lines, with the correct color for each one although the label is not shown. Such label, though, is indeed shown if I comment out the lineFeature.style={...} lines. In this case, though, the lines are all black. I'm quite new at OpenLayers, so I'm not sure: what am I doing wrong?

For reference, I started out with this example.

2

I believe you are designating a style on each loop pass which then does not use the default render intent on the stylemap. Before you do a lineFeature.style = ..., Try extending the default style in your stylemap and apply it to the feature instead of telling the feature to use a style which does not have any label info on it.

Update

remove the lineFeature.style = ...

add the color value to your feature attributes:

lineFeature.attributes = {
    label: 'lalal',
    color: strokeColors[j%4]
};

and replace this for your linesLayer constructor:

var linesLayer = new OpenLayers.Layer.Vector("Lines Layer", {
    styleMap: new OpenLayers.StyleMap({'default':{
        pointRadius: 6,
        labelYOffset: -15,
        labelOutlineColor: "white",
        labelOutlineWidth: 3,
        label: "${label}",
        strokeColor: "${color}",
        strokeOpacity: 1.0,
        strokeWidth: 6
    }})
});
  • Thanks for the answer. I tried your suggestion, but the result is the same: colored line, but no point nor label. – user8440 Jun 26 '12 at 13:07
  • Do you have the values of the lines array so I can test it? – CaptDragon Jun 26 '12 at 13:10
  • they're retrieved from a webservice. gimme a few mins and i'll try to set up a fiddle – user8440 Jun 26 '12 at 13:13
  • Or if you could just paste in the resulting JSON of the webservice, that would work, if possible. – CaptDragon Jun 26 '12 at 13:14
  • it's actually a massive xml, cause it was built for something else. (which is source of other pains, but nevermind) – user8440 Jun 26 '12 at 13:15

Your Answer

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