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

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)?

share|improve this question
up vote 5 down vote accepted

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

measureControl = new OpenLayers.Control.Measure(
        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{
    'measure': function(evt) {
        $('.foo').html(measureControl.getCustomLength(evt, true));
    'measurepartial': function(evt) {
        $('.foo').html(measureControl.getCustomLength(evt, false));
share|improve this answer
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 '15 at 14:50

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.