Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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
    }})
});
share|improve this answer
    
Thanks for the answer. I tried your suggestion, but the result is the same: colored line, but no point nor label. –  Stephan 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 –  Stephan 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) –  Stephan Jun 26 '12 at 13:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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