"panTo should use tween if new center is in the current bounds + a ratio".

Add a bounds.scale method (takes a ratio and an optional center) and 
call it from the panTo to give a ratio we can pan inside of. Patch by
sbenthall, r=me,elemoine (Closes #1341) 


git-svn-id: http://svn.openlayers.org/trunk/openlayers@7678 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
crschmidt
2008-08-02 01:56:36 +00:00
parent 61225ed98f
commit f1882f0efa
3 changed files with 69 additions and 1 deletions

View File

@@ -219,6 +219,48 @@ OpenLayers.Bounds = OpenLayers.Class({
(this.bottom + this.top) / 2);
},
/**
* Method: scale
* Scales the bounds around a pixel or lonlat. Note that the new
* bounds may return non-integer properties, even if a pixel
* is passed.
*
* Parameters:
* ratio - {Float}
* origin - {<OpenLayers.Pixel> or <OpenLayers.LonLat>}
* Default is center.
*
* Returns:
* {<OpenLayers.Bound>} A new bounds that is scaled by ratio
* from origin.
*/
scale: function(ratio, origin){
if(origin == null){
origin = this.getCenterLonLat();
}
var bounds = [];
var origx,origy;
// get origin coordinates
if(origin.CLASS_NAME == "OpenLayers.LonLat"){
origx = origin.lon;
origy = origin.lat;
} else {
origx = origin.x;
origy = origin.y;
}
var left = (this.left - origx) * ratio + origx;
var bottom = (this.bottom - origy) * ratio + origy;
var right = (this.right - origx) * ratio + origx;
var top = (this.top - origy) * ratio + origy;
return new OpenLayers.Bounds(left, bottom, right, top);
},
/**
* APIMethod: add
*

View File

@@ -196,6 +196,13 @@ OpenLayers.Map = OpenLayers.Class({
*/
zoom: 0,
/**
* Property: panRatio
* {Float} The ratio of the current extent within
* which panning will tween.
*/
panRatio: 1.5,
/**
* Property: viewRequestID
* {String} Used to store a unique identifier that changes when the map
@@ -1368,7 +1375,7 @@ OpenLayers.Map = OpenLayers.Class({
* lonlat - {<OpenLayers.Lonlat>}
*/
panTo: function(lonlat) {
if (this.panMethod && this.getExtent().containsLonLat(lonlat)) {
if (this.panMethod && this.getExtent().scale(this.panRatio).containsLonLat(lonlat)) {
if (!this.panTween) {
this.panTween = new OpenLayers.Tween(this.panMethod);
}

View File

@@ -557,6 +557,25 @@
t.ok(bounds == null, "returns null on erroneous add operation (null y)");
}
function test_Bounds_scale(t) {
t.plan(3);
origBounds = new OpenLayers.Bounds(1,2,3,4);
bounds = origBounds.scale(2);
var b = new OpenLayers.Bounds(0,1,4,5);
t.ok(bounds.equals(b), "Bounds scale correctly with default origin at center")
var origin = new OpenLayers.Pixel(0,1);
bounds = origBounds.scale(2,origin);
b = new OpenLayers.Bounds(2,3,6,7);
t.ok(bounds.equals(b), "Bounds scale correctly with offset origin");
origin = new OpenLayers.Pixel(5,1);
bounds = bounds.scale(2, origin);
b = new OpenLayers.Bounds(-1, 5, 7, 13);
t.ok(bounds.equals(b), "Bounds scale correctly with offset origin");
}
</script>
</head>