[OpenLayers-Users] measure / measurepartial

Paul Spencer pagameba at gmail.com
Fri Sep 18 09:47:13 EDT 2009


Rich,

I've done this recently, I've attached the code.  Its mixed with some  
application-specific stuff and is configured to show text distances on  
each segment but it does show how to do something differently on move  
vs click

Cheers

Paul

-------------- next part --------------
OpenLayers.Control.Measure.prototype.EVENT_TYPES = ['measure', 'measurepartial', 'measuredynamic'];
measureControl = new OpenLayers.Control.Measure(
    OpenLayers.Handler.Path, {
        textNodes: null,
        showDistances: !Browser.Engine.trident,
        persist: true,
        geodesic: true,
        partialDelay: 300,
        callbacks: {
            create: function() {
                this.showDistances = !Browser.Engine.trident;
                this.textNodes = [];
                rulerLayer.layer.removeFeatures(
                    rulerLayer.layer.features);
            },
            modify: function(point, line) {
                // introduce a delay for IE browsers so they will
                // draw the first segment
                if (!this.showDistances) {
                    return;
                }
                var len = line.geometry.components.length;
                var from = line.geometry.components[len - 2];
                var to = line.geometry.components[len - 1];
                var ls = new OpenLayers.Geometry.LineString([
                      from,to]);
                var dist = this.getBestLength(ls);
                if (!dist[0]) {
                    return;
                }
                var total = this.getBestLength(line.geometry);
                var label;
                if (dist[1] == 'm') {
                    label = dist[0].toFixed(0)+dist[1];
                } else {
                    label = dist[0].toFixed(2)+dist[1];
                }
                var textNode = this.textNodes[len-2] || null;
                if (textNode && !textNode.layer) {
                    this.textNodes.pop();
                    textNode = null;
                }
                if (!textNode) {
                    var c = ls.getCentroid();
                    textNode = new OpenLayers.Feature.Vector(
                        new OpenLayers.Geometry.Point(c.x,c.y),
                        {}, {
                            label: '', 
                            fontColor: "#00F",
                            fontSize: "14px",
                            fontFamily: "Arial",
                            fontWeight: "bold",
                            labelAlign: "cm"
                        });
                    this.textNodes.push(textNode);
                    rulerLayer.layer.addFeatures([textNode]);
                }
                textNode.geometry.x = (from.x+to.x)/2;
                textNode.geometry.y = (from.y+to.y)/2;
                textNode.style.label = label;
                textNode.layer.drawFeature(textNode);
                this.events.triggerEvent('measuredynamic', {
                    measure: dist[0],
                    total: total[0],
                    units: dist[1],
                    order: 1,
                    geometry: ls
                });
            }
        },
        handlerOptions: {
            layerOptions: {
                styleMap: new OpenLayers.StyleMap({'default': {
                    strokeColor: "#00F",
                    strokeOpacity: 0.3,
                    strokeWidth: 5,
                    strokeLinecap: 'square',
                    fillColor: "#FFF",
                    fillOpacity: 0,
                    pointRadius: 0,
                    pointerEvents: "visiblePainted",
                    label : ""
                }})
            }
        }
    }
);
var hasMeasure = false;
measureControl.events.on({
    measurepartial: function(evt) {
        measureControl.showDistances = true;
        if (hasMeasure) {
            $('RulerTotal').set('html','');
            hasMeasure = false;
        }
    },
    measuredynamic: function(evt) {
        if (hasMeasure) {
            $('RulerTotal').set('html','');
            hasMeasure = false;
        }
        var measure = evt.total;
        var units = evt.units;
        var label;
        if (units == 'm') {
            label = measure.toFixed(0)+units;
        } else {
            label = measure.toFixed(2)+units;
        }
        $('RulerTotal').set('html', label);
    },
    measure: function(evt) {
        var measure = evt.measure;
        var units = evt.units;
        var label;
        if (units == 'm') {
            label = measure.toFixed(0)+units;
        } else {
            label = measure.toFixed(2)+units;
        }
        $('RulerTotal').set('html', label);
        hasMeasure = true;
        var features = [];
        measureControl.handler.layer.features.each(function(f){
            features.push(f.clone());
        });
        rulerLayer.layer.addFeatures(features);
        measureControl.cancel();
    }
});
-------------- next part --------------


On 2009-09-18, at 9:39 AM, Richard Greenwood wrote:

> On Fri, Sep 18, 2009 at 1:47 AM, Andreas Hocevar  
> <ahocevar at opengeo.org> wrote:
>> Hey-
>>
>> Richard Greenwood wrote:
>>> I'm trying to make sense of measure versus measurepartial event  
>>> types
>>> in OL 2.8. Mouse moves and single clicks both report an event type  
>>> of
>>> "measurepartial". Only a double click report a "measure" event  
>>> type. I
>>> would like to distinguish between an mouse move (rubber band) and a
>>> single click (which creates a new vertex in the line string). The  
>>> only
>>> way I have figured out so far to do this is to monitor the length of
>>> event.geometry.components but it seems like there should be a better
>>> way, which possibly I am missing.
>>>
>>
>> The Measure control does not register a callback for the handler's
>> modify event, which means that you normally receive a measurepartial
>> event on a single click and a measure event on double click. If you  
>> also
>> receive measurepartial on mouse move, then you have configured your
>> control with a callback on the handler's modify event,
>
> Andreas - Thanks for the reply. I did configure a callback for the
> modify event as you noted. Sorry not to have mentioned that in my
> original post. My problem is that I am unable to distinguish between a
> mouse move and a single click. I need to do different things in
> response to these two types of events.
>
> Thanks,
> Rich
>
> -- 
> Richard Greenwood
> richard.greenwood at gmail.com
> www.greenwoodmap.com
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users



More information about the Users mailing list