bubbling up the 'closest' parameter to the higher-level map object api calls zoomToScale() and zoomToExtent(). Includes thorough tests. r=cr5 (Closes #1250)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@7889 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
euzuro
2008-08-29 06:08:32 +00:00
parent ec2b9b5289
commit 07ac8f322a
2 changed files with 91 additions and 4 deletions

View File

@@ -1940,8 +1940,13 @@ OpenLayers.Map = OpenLayers.Class({
*
* Parameters:
* bounds - {<OpenLayers.Bounds>}
* closest - {Boolean} Find the zoom level that most closely fits the
* specified bounds. Note that this may result in a zoom that does
* not exactly contain the entire extent.
* Default is false.
*
*/
zoomToExtent: function(bounds) {
zoomToExtent: function(bounds, closest) {
var center = bounds.getCenterLonLat();
if (this.baseLayer.wrapDateLine) {
var maxExtent = this.getMaxExtent();
@@ -1965,7 +1970,7 @@ OpenLayers.Map = OpenLayers.Class({
//
center = bounds.getCenterLonLat().wrapDateLine(maxExtent);
}
this.setCenter(center, this.getZoomForExtent(bounds));
this.setCenter(center, this.getZoomForExtent(bounds, closest));
},
/**
@@ -1995,8 +2000,13 @@ OpenLayers.Map = OpenLayers.Class({
*
* Parameters:
* scale - {float}
* closest - {Boolean} Find the zoom level that most closely fits the
* specified scale. Note that this may result in a zoom that does
* not exactly contain the entire extent.
* Default is false.
*
*/
zoomToScale: function(scale) {
zoomToScale: function(scale, closest) {
var res = OpenLayers.Util.getResolutionFromScale(scale,
this.baseLayer.units);
var size = this.getSize();
@@ -2008,7 +2018,7 @@ OpenLayers.Map = OpenLayers.Class({
center.lat - h_deg / 2,
center.lon + w_deg / 2,
center.lat + h_deg / 2);
this.zoomToExtent(extent);
this.zoomToExtent(extent, closest);
},
/********************************************************/

View File

@@ -1094,7 +1094,84 @@
t.ok(gRestricted == options.restricted, "when valid options argument, 'options.restricted' passed to map.getMaxExtent()");
}
function test_Map_zoomToScale(t) {
t.plan(4);
var m = {
'baseLayer': { 'units': {} },
'getSize': function() { return {'w': 10, 'h': 15}; },
'getCenter': function() { return {'lon': -5, 'lat': -25}; },
'zoomToExtent': function(extent, closest) {
t.ok(extent.equals(g_ExpectedExtent), "extent correctly calculated for zoomToExtent()");
t.ok(closest == g_Closest, "closest correctly passed on to zoomToExtent()");
}
}
var temp = OpenLayers.Util.getResolutionFromScale;
OpenLayers.Util.getResolutionFromScale = function(scale, units) {
t.ok(scale == g_Scale, "scale parameter correctly passed to getResolutionFromScale");
t.ok(units == m.baseLayer.units, "map's baselayer's units parameter correctly passed to getResolutionFromScale");
return 1000;
};
g_ExpectedExtent = new OpenLayers.Bounds(-5005,-7525,4995,7475);
g_Scale = {};
g_Closest = {};
var args = [g_Scale, g_Closest];
OpenLayers.Map.prototype.zoomToScale.apply(m, args);
OpenLayers.Util.getResolutionFromScale = temp;
}
function test_Map_zoomToExtent(t) {
t.plan(8);
var m = {
'baseLayer': {
'wrapDateLine': false
},
'setCenter': function(center, zoomLevel) {
g_Center = center;
g_ZoomLevel = zoomLevel;
},
'getZoomForExtent': function(bounds, closest) {
t.ok(bounds.equals(g_ToCenterBounds), "bounds correctly passed into getZoomForExtent()");
t.ok(closest == g_Closest, "closest correctly passed along to getZoomForExtent()");
return g_ZoomLevelReturn;
}
};
//no wrapDateLine
g_ZoomLevelReturn = {};
g_Bounds = new OpenLayers.Bounds(-20,-15,0,5);
g_ExpectedCenter = new OpenLayers.LonLat(-10,-5);
g_Closest = {};
g_ToCenterBounds = g_Bounds;
var args = [g_Bounds, g_Closest];
OpenLayers.Map.prototype.zoomToExtent.apply(m, args);
t.ok(g_Center.equals(g_ExpectedCenter), "setCenter called on correct center");
t.ok(g_ZoomLevel == g_ZoomLevelReturn, "correctly passes along zoom level as returned from getZoomForExtent()");
//wrapDateLine
m.baseLayer.wrapDateLine = true;
m.getMaxExtent = function() { return new OpenLayers.Bounds(-200,-200,200,200); };
g_ZoomLevelReturn = {};
g_BoundsCenter = {};
g_Bounds = new OpenLayers.Bounds(160,-60,-60,60);
g_ExpectedCenter = new OpenLayers.LonLat(-150,0);
g_Closest = {};
g_ToCenterBounds = new OpenLayers.Bounds(160,-60,340,60);
var args = [g_Bounds, g_Closest];
OpenLayers.Map.prototype.zoomToExtent.apply(m, args);
t.ok(g_Center.equals(g_ExpectedCenter), "setCenter called on correct center");
t.ok(g_ZoomLevel == g_ZoomLevelReturn, "correctly passes along zoom level as returned from getZoomForExtent()");
}
</script>
</head>