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.