[OpenLayers-Users] Linestring Label in the middle of the linestring

Claudio V. claudio.carvalho at lemaf.ufla.br
Thu Aug 1 11:48:40 PDT 2013


Hi Mikhael, take a look at a question from few days ago here on the list,
named "How to display polygon length dimensions".
Javier Mamano replied with a link to one of his libs that displays the
length of a line in the middle of it:
http://jorix.github.io/OL-DynamicMeasure/examples/measure-dynamic.html
Maybe you could take a look at his code =]
Att.
    Cláudio



2013/8/1 Mikhaél Santos <mikhael.santos at xlm.pt>

> Hi guys,****
>
> I wanted to know if it’s possible to dynamically set a linestrings label
> to the center of the linestring. ****
>
> Currently I have something like this:****
>
>         labelDeltaPixels: function (feature) {****
>
>             var vert = feature.geometry.getVertices();****
>
>             var startPoint = vert[0],****
>
>                 middlePoint;****
>
> //If more than 1 point get a middle point****
>
> //else calculate the middle coordenate****
>
>             if(vert.length > 2){****
>
>                 middlePoint = vert[Math.floor(vert.length / 2)];****
>
>             }else {****
>
>                 middlePoint = new OpenLayers.Geometry.Point((vert[0].x +
> vert[1].x)/2, (vert[0].y + vert[1].y)/2)****
>
>             }****
>
>           //This is where I am having problems the values obtain from
> pixelStart and PixelMiddle don’t coincide with the middle of the
> linestring, it’s not close at all, the label continues at the start point.
> ****
>
>             var pixelStart = feature.layer.map.getPixelFromLonLat(new
> OpenLayers.LonLat(startPoint.x.toFixed(5) , startPoint.y.toFixed(5) ));***
> *
>
>             var pixelMiddle =  feature.layer.map.getPixelFromLonLat(new
> OpenLayers.LonLat(middlePoint.x.toFixed(5) , middlePoint.y.toFixed(5) ));*
> ***
>
>             var deltaX = pixelMiddle.x- pixelStart.x;****
>
>             var deltaY = pixelStart.y - pixelMiddle.y;****
>
>             return {****
>
>                 x: deltaX,****
>
>                 y: deltaY****
>
>             };****
>
>         }****
>
> ** **
>
> Any help is appreciated.****
>
> Thanks****
>
> _______________________________________________
> Users mailing list
> Users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/openlayers-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/openlayers-users/attachments/20130801/c26884fe/attachment-0001.html>


More information about the Users mailing list