Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

1 Answer 1

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: 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;
}

map.addControl(measureControl); 

// 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));
    }
});
share|improve this answer
1  
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 at 5:51

Your Answer

 
discard

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.