OpenLayers OpenLayers

Changeset 7593

Show
Ignore:
Timestamp:
07/29/08 19:30:32 (4 months ago)
Author:
crschmidt
Message:

Support for moving vector features a given number of pixels, or to a given lon/lat, using a move() method. Patch from sbenthall. (Closes #1326)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers/Feature/Vector.js

    r7395 r7593  
    216216    destroyPopup: function() { 
    217217        // pass 
     218    }, 
     219 
     220    /** 
     221     * Method: move 
     222     * Moves the feature and redraws it at its new location 
     223     * 
     224     * Parameters: 
     225     * state - {OpenLayers.LonLat or OpenLayers.Pixel} the 
     226     *         location to which to move the feature. 
     227     */ 
     228    move: function(location) { 
     229 
     230        if(!this.layer || !this.geometry.move){ 
     231            //do nothing if no layer or immoveable geometry 
     232            return; 
     233        } 
     234 
     235        var pixel; 
     236        if (location.CLASS_NAME == "OpenLayers.LonLat") { 
     237            pixel = this.layer.getViewPortPxFromLonLat(location); 
     238        } else { 
     239            pixel = location; 
     240        } 
     241         
     242        var lastPixel = this.layer.getViewPortPxFromLonLat(this.geometry.getBounds().getCenterLonLat()); 
     243        var res = this.layer.map.getResolution(); 
     244        this.geometry.move(res * (pixel.x - lastPixel.x), 
     245                           res * (lastPixel.y - pixel.y)); 
     246        this.layer.drawFeature(this); 
     247        return lastPixel; 
    218248    }, 
    219249     
  • trunk/openlayers/tests/Feature/Vector.html

    r7252 r7593  
    8787    } 
    8888         
     89    function test_Feature_Vector_move(t) { 
     90        t.plan(3); 
    8991 
     92        var oldx = 26; 
     93        var oldy = 14; 
     94        var newx = 6; 
     95        var newy = 4; 
     96        var res = 10; 
     97 
     98        var geometry = new OpenLayers.Geometry.Point(oldx, 
     99                                                     oldy); 
     100 
     101        var drawn = false; 
     102 
     103        feature = new OpenLayers.Feature.Vector(geometry); 
     104 
     105        feature.layer = { 
     106            getViewPortPxFromLonLat : function(lonlat){ 
     107                return new OpenLayers.Pixel(lonlat.lon,lonlat.lat); 
     108            }, 
     109            map: { 
     110                getResolution: function(){ 
     111                    return res; 
     112                } 
     113            }, 
     114            drawFeature: function(){ 
     115                drawn = true; 
     116            } 
     117        } 
     118 
     119        var pixel = new OpenLayers.Pixel(newx,newy) 
     120 
     121        feature.move(pixel); 
     122 
     123        geometry = feature.geometry; 
     124 
     125        t.ok(drawn, "The feature is redrawn after the move"); 
     126        t.eq(geometry.x, res * (newx - oldx) + oldx, "New geometry has proper x coordinate"); 
     127        t.eq(geometry.y, res * (oldy - newy) + oldy, "New geometry has proper y coordinate"); 
     128    } 
     129         
    90130 
    91131  </script>