Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


share|improve this question

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 =      OpenLayers.LonLat(startPoint.x, startPoint.y));
    var pixelMiddle = 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 =;
            if (zoom < self.centreLabelsZoom) {
                return 0;

            if (f.geometry) {
                if (!self.centrelineMiddleCoordsMap[zoom]) {
                    self.centrelineMiddleCoordsMap[zoom] = {};
                if (!self.centrelineMiddleCoordsMap[zoom][]) {
                    self.centrelineMiddleCoordsMap[zoom][] =     self.labelDeltaPixels(f);
                return self.centrelineMiddleCoordsMap[zoom][].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])); = {
                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()); = {
                fontFamily: "arial, monospace",
                fontWeight: "bold",
                fontColor: "red",
                label : length +"m",
                labelAlign: "tr"//set to top right
share|improve this answer
This is the best answer. Thanks a lot :) – Arvind07 May 28 at 4:11

Your Answer


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.