From 7736e12d33e9c882a2c05bcb3bee16adf7f03762 Mon Sep 17 00:00:00 2001 From: crschmidt Date: Tue, 29 Jul 2008 23:30:32 +0000 Subject: [PATCH] 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) git-svn-id: http://svn.openlayers.org/trunk/openlayers@7593 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Feature/Vector.js | 30 ++++++++++++++++++++++++ tests/Feature/Vector.html | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/lib/OpenLayers/Feature/Vector.js b/lib/OpenLayers/Feature/Vector.js index 4b6b90d2c8..127ef48258 100644 --- a/lib/OpenLayers/Feature/Vector.js +++ b/lib/OpenLayers/Feature/Vector.js @@ -216,6 +216,36 @@ OpenLayers.Feature.Vector = OpenLayers.Class(OpenLayers.Feature, { destroyPopup: function() { // pass }, + + /** + * Method: move + * Moves the feature and redraws it at its new location + * + * Parameters: + * state - {OpenLayers.LonLat or OpenLayers.Pixel} the + * location to which to move the feature. + */ + move: function(location) { + + if(!this.layer || !this.geometry.move){ + //do nothing if no layer or immoveable geometry + return; + } + + var pixel; + if (location.CLASS_NAME == "OpenLayers.LonLat") { + pixel = this.layer.getViewPortPxFromLonLat(location); + } else { + pixel = location; + } + + var lastPixel = this.layer.getViewPortPxFromLonLat(this.geometry.getBounds().getCenterLonLat()); + var res = this.layer.map.getResolution(); + this.geometry.move(res * (pixel.x - lastPixel.x), + res * (lastPixel.y - pixel.y)); + this.layer.drawFeature(this); + return lastPixel; + }, /** * Method: toState diff --git a/tests/Feature/Vector.html b/tests/Feature/Vector.html index 8cdb92e6ee..7ceffcc8c7 100644 --- a/tests/Feature/Vector.html +++ b/tests/Feature/Vector.html @@ -86,7 +86,47 @@ "clone can clone geometry-less features"); } + function test_Feature_Vector_move(t) { + t.plan(3); + var oldx = 26; + var oldy = 14; + var newx = 6; + var newy = 4; + var res = 10; + + var geometry = new OpenLayers.Geometry.Point(oldx, + oldy); + + var drawn = false; + + feature = new OpenLayers.Feature.Vector(geometry); + + feature.layer = { + getViewPortPxFromLonLat : function(lonlat){ + return new OpenLayers.Pixel(lonlat.lon,lonlat.lat); + }, + map: { + getResolution: function(){ + return res; + } + }, + drawFeature: function(){ + drawn = true; + } + } + + var pixel = new OpenLayers.Pixel(newx,newy) + + feature.move(pixel); + + geometry = feature.geometry; + + t.ok(drawn, "The feature is redrawn after the move"); + t.eq(geometry.x, res * (newx - oldx) + oldx, "New geometry has proper x coordinate"); + t.eq(geometry.y, res * (oldy - newy) + oldy, "New geometry has proper y coordinate"); + } +