[GIS] Can you get the individual segment length from OpenLayers2 measure class

openlayers-2

OpenLayers has a OpenLayers.Control.Measure class to implement a measure tool.

There is a measure and measure-partial event, both which are passed a variable containing the total distance. Is it possible to get the last segment length?

CLARIFY: When I say last segment length… you see how you can draw 2 or 3 lines in one go and it will give you the total distance drawn across all lines? That's great. What I'm after is the length of the last line the user drew only.

Best Answer

see the exemple on openlayers website and have a glance to the source code, adapting it you will be able to get the length of last segment

I would quickly adapt the code like that (not tested) :

function handleMeasurements(event) {
        var geometry = event.geometry;
        var units = event.units;
        var order = event.order;
        var measure = event.measure;
        var element = document.getElementById('output');

        var out = "";
        if(order == 1) {
            out += measure.toFixed(3);
        } else {
            out += measure.toFixed(3);
        }
        alert("Latest segment length : " + (element.value - out))
        element.innerHTML = out;
}