# OpenLayers measure tool - instant measurement

I need to use measure tool to measure distances (instant measurement!) in my application. Instant measurement example can be found here. But the main problem is that when I use instant measurement tool and try to measure distance, it sums all the pieces and shows sum. So the question: how can I instantly show only the current piece distance (I mean not to sum up all pieces distances)?

-

Here is copy-paste from one of my old applications. EDIT: I modified getCustomLength, so it should work with different coordinate systems. Fiddle: http://jsfiddle.net/A78Zu/

``````measureControl = new OpenLayers.Control.Measure(
OpenLayers.Handler.Path,
{
immediate: true,
persist: true
}
);

measureControl.getCustomLength = function(evt, onlySum) {
var idx = evt.geometry.components.length;

if (idx < 2)
return '';

var geom = new OpenLayers.Geometry.LineString([
evt.geometry.components[idx - 2],
evt.geometry.components[idx - 1]
]);

var lastLengthArr = this.getBestLength(geom);

var str = '';

if (idx > 2 || onlySum)
str += 'Total: ' + (evt.units === 'km' ? evt.measure.toFixed(3) : evt.measure.toFixed(1)) + ' ' + evt.units + '&nbsp;&nbsp;&nbsp;';

if (!onlySum)
str += 'Last segment: ' + (lastLengthArr[1] === 'km' ?  lastLengthArr[0].toFixed(3) : lastLengthArr[0].toFixed(1)) + ' ' + lastLengthArr[1];

return str;
}

// Update content in .foo div
measureControl.events.on({
'measure': function(evt) {
\$('.foo').html(measureControl.getCustomLength(evt, true));
},
'measurepartial': function(evt) {
\$('.foo').html(measureControl.getCustomLength(evt, false));
}
});
``````
-
Hey, do you have the same method for polygons? This one works only for lines. – Bob Dec 4 '12 at 12:33
Problem solved. – Bob Dec 4 '12 at 13:56
Holy sht you're a guru! Solve my problem too. By the way, 900913 means "google". – Magno C Dec 4 '12 at 15:12
@Bob... Do you have similar method for the polygons? As you mentioned 'Problem solved'. – muzaffar Jan 22 '14 at 5:51
Folks, how can I use this to measure the AREA of a polygon? – user3657279 Feb 6 at 14:50