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.

Is there anyone can put label in the middle of the linestring dynamically? I've used labelAlign option. I've changed this value from bc, cc, cm, etc. But it doesn't work at all. There's also option for x offset and y offset. But I don't know how to calculate the right position so the label can show at the middle of linestring. Not at the first line string.

Do you have any idea how to do it?

Thanks,

share|improve this question

3 Answers 3

Check out this answer here may help you if u glance at the code.

i hope it helps you...

share|improve this answer

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

It's poccinle to calculate position manually;

  1. Define function which will calculate deltaX and deltaY:

..

this.labelDeltaPixels = function (f) {
    var vert = f.geometry.getVertices();
    var startPoint = vert[0];
    var middlePoint = vert[Math.floor(vert.length/2)];
    var pixelStart = self.mapPanel.map.getPixelFromLonLat(new      OpenLayers.LonLat(startPoint.x, startPoint.y));
    var pixelMiddle = self.mapPanel.map.getPixelFromLonLat(new OpenLayers.LonLat(middlePoint.x, middlePoint.y));
    var deltaX = pixelMiddle.x - pixelStart.x;
    var deltaY = pixelStart.y - pixelMiddle.y;
    return {x: deltaX, y: deltaY};
}

2.define context for template

var context = {
          getLableOffsetX: function(f) {
            var zoom = self.mapPanel.map.getZoom();
            if (zoom < self.centreLabelsZoom) {
                return 0;
            }

            if (f.geometry) {
                if (!self.centrelineMiddleCoordsMap[zoom]) {
                    self.centrelineMiddleCoordsMap[zoom] = {};
                }
                if (!self.centrelineMiddleCoordsMap[zoom][f.id]) {
                    self.centrelineMiddleCoordsMap[zoom][f.id] =     self.labelDeltaPixels(f);
                }
                return self.centrelineMiddleCoordsMap[zoom][f.id].x;
                    return 0;
            } else {
                return 0;
            }
        },
        getLableOffsetY: function (){//similar as for x}

}

  1. define template. var template = {strokeColor:"${getColor}", label: "${getLabel}", cursor: "crosshair", fontSize: '11px', labelAlign: 'cm', labelXOffset: '${getLableOffsetX}', labelYOffset: '${getLableOffsetY}'};

  2. define style and stylemap var style = new OpenLayers.Style(template, {context: context, title: 'default'}); var styleSelect = new OpenLayers.Style({'strokeColor': self.colors['centreline'].selectColor});

share|improve this answer

Although it's quite old question, this solution may help other looking for similar problem.

To display the line label at center of line, you need to create another point feature at the centroid of the lineFeature. The code for this is as follows

var VectorLayer = new OpenLayers.Layer.Vector("LineVector",{displayInLayerSwitcher: false,renderers: ["Canvas", "SVG", "VML"]});
//Create line geometry
var lineString = new OpenLayers.Geometry.LineString([source,target]);
//create line feature
var lineFeature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Collection([lineString]));
        lineFeature.style = {
                strokeColor: '#ff0000', 
                  strokeOpacity: 0.7,
                  strokeWidth: 2
            };//after this create label feature i.e. point feature
        var lableFeature = new OpenLayers.Feature.Vector(lineString.getCentroid(true));/*Important pass true parameter otherwise it will return start point as centroid*/

        var length = Math.round(lineString.getLength());
        lableFeature.style = {
                fontFamily: "arial, monospace",
                fontWeight: "bold",
                fontColor: "red",
                label : length +"m",
                labelAlign: "tr"//set to top right
        };      
        VectorLayer.addFeatures([lineFeature,lableFeature]);
        VectorLayer.redraw();
        map.addLayers([VectorLayer]);
share|improve this answer

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.