OpenLayers OpenLayers

Ticket #1125: OpenLayersSmoothDrag.patch

File OpenLayersSmoothDrag.patch, 3.3 kB (added by haakeyar, 1 year ago)
  • tests/Control/test_DragPan.html

    old new  
    3232        map.events.triggerEvent('mousedown', {'type':'mousedown', 'xy':new OpenLayers.Pixel(0,0), 'which':1}); 
    3333        map.events.triggerEvent('mousemove', {'type':'mousemove', 'xy':new OpenLayers.Pixel(5,5), 'which':1}); 
    3434        map.events.triggerEvent('mouseup', {'type':'mouseup', 'xy':new OpenLayers.Pixel(5,5), 'which':1}); 
    35          
    36         t.eq(map.getCenter().lat, res * 5, "Lat is " + (res * 5) + " after drag"); 
    37         t.eq(map.getCenter().lon, res * -5, "Lon is " + (res * -5) + " after drag"); 
     35        t.delay_call(0.2, function() 
     36        { 
     37          t.eq(map.getCenter().lat, res * 5, "Lat is " + (res * 5) + " after drag"); 
     38          t.eq(map.getCenter().lon, res * -5, "Lon is " + (res * -5) + " after drag"); 
     39        }); 
    3840    } 
    3941    function test_Control_DragPan_click(t) { 
    4042        t.plan(1); 
  • lib/OpenLayers/Control/DragPan.js

    old new  
    2727    panned: false, 
    2828     
    2929    /** 
     30     * Property: deltaX 
     31     * {Integer} The number of x pixels that has been moved since the last update of the map 
     32     */ 
     33    deltaX: 0, 
     34     
     35    /** 
     36     * Property: deltaX 
     37     * {Integer} The number of y pixels that has been moved since the last update of the map 
     38     */ 
     39    deltaY: 0, 
     40     
     41    /** 
     42     * Property: updateMapTimeout 
     43     * {Integer} The identifier for the timeout that will update the map. 
     44     */ 
     45    updateMapTimeout: null,  
     46    
     47    /** 
    3048     * Method: draw 
    3149     * Creates a Drag handler, using <OpenLayers.Control.PanMap.panMap> and 
    3250     * <OpenLayers.Control.PanMap.panMapDone> as callbacks. 
     
    4462    */ 
    4563    panMap: function(xy) { 
    4664        this.panned = true; 
    47         var deltaX = this.handler.last.x - xy.x; 
    48         var deltaY = this.handler.last.y - xy.y; 
     65        this.deltaX += this.handler.last.x - xy.x; 
     66        this.deltaY += this.handler.last.y - xy.y; 
     67        if(this.updateMapTimeout == null) // If there is already a timeout waiting to update the map, do nothing. If there isn't, create one. 
     68        { 
     69            this.updateMapTimeout = setTimeout(OpenLayers.Function.bind(this.updateMap, this), 10); // Wait 10 ms and update the map. 
     70        } 
     71    }, 
     72     
     73    /** 
     74     * Method: updateMap 
     75     * Update the map to the new position set by panMap. 
     76     * Called automatically from panMap in a timeout. 
     77     * 
     78     * @private 
     79     */ 
     80    updateMap: function() 
     81    { 
    4982        var size = this.map.getSize(); 
    50         var newXY = new OpenLayers.Pixel(size.w / 2 + deltaX, 
    51                                          size.h / 2 + deltaY); 
     83        var newXY = new OpenLayers.Pixel(size.w / 2 + this.deltaX, 
     84                                         size.h / 2 + this.deltaY); 
     85         
     86        // Clean up, so that a new timeout will be created on the next move event 
     87        this.updateMapTimeout = null; 
     88        this.deltaX = 0; 
     89        this.deltaY = 0; 
     90         
    5291        var newCenter = this.map.getLonLatFromViewPortPx( newXY ); 
    5392        this.map.setCenter(newCenter, null, this.handler.dragging); 
    5493    },