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
This commit is contained in:
@@ -217,6 +217,36 @@ OpenLayers.Feature.Vector = OpenLayers.Class(OpenLayers.Feature, {
|
|||||||
// pass
|
// 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
|
* Method: toState
|
||||||
* Sets the new state
|
* Sets the new state
|
||||||
|
|||||||
@@ -86,6 +86,46 @@
|
|||||||
"clone can clone geometry-less features");
|
"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");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user