[OpenLayers-Commits] r4768 - in sandbox/euzuro/zoomToResolution: lib/OpenLayers tests
commits at openlayers.org
commits at openlayers.org
Wed Oct 3 01:27:49 EDT 2007
Author: euzuro
Date: 2007-10-03 01:27:48 -0400 (Wed, 03 Oct 2007)
New Revision: 4768
Modified:
sandbox/euzuro/zoomToResolution/lib/OpenLayers/Layer.js
sandbox/euzuro/zoomToResolution/lib/OpenLayers/Map.js
sandbox/euzuro/zoomToResolution/tests/test_Layer.html
Log:
add the 'closest' feature from r4381, but in a safer way... only invoke it on changing baselayers. includes a couple tests.
Modified: sandbox/euzuro/zoomToResolution/lib/OpenLayers/Layer.js
===================================================================
--- sandbox/euzuro/zoomToResolution/lib/OpenLayers/Layer.js 2007-10-03 03:32:17 UTC (rev 4767)
+++ sandbox/euzuro/zoomToResolution/lib/OpenLayers/Layer.js 2007-10-03 05:27:48 UTC (rev 4768)
@@ -730,20 +730,24 @@
*
* Parameters:
* bounds - {<OpenLayers.Bounds>}
+ * closest - {Boolean} Find the zoom level that corresponds to the absolute
+ * closest for the given extent, which may result in a zoom whose
+ * that does not exactly contain the entire extent.
+ * Default is false.
*
* Returns:
* {Integer} The index of the zoomLevel (entry in the resolutions array)
- * that still contains the passed-in extent. We do this by calculating
- * the ideal resolution for the given exteng (based on the map size)
- * and then find the smallest resolution that is greater than this
- * ideal resolution.
+ * for the passed-in extent. We do this by calculating the ideal
+ * resolution for the given extent (based on the map size) and then
+ * calling getZoomForResolution(), passing along the 'closest'
+ * parameter.
*/
- getZoomForExtent: function(extent) {
+ getZoomForExtent: function(extent, closest) {
var viewSize = this.map.getSize();
var idealResolution = Math.max( extent.getWidth() / viewSize.w,
extent.getHeight() / viewSize.h );
- return this.getZoomForResolution(idealResolution);
+ return this.getZoomForResolution(idealResolution, closest);
},
/**
@@ -763,20 +767,36 @@
*
* Parameters:
* resolution - {Float}
+ * closest - {Boolean} Find the zoom level that corresponds to the absolute
+ * closest resolution, which may result in a zoom whose corresponding
+ * resolution is actually smaller than we would have desired (if this
+ * is being called from a getZoomForExtent() call, then this means that
+ * the returned zoom index might not actually contain the entire
+ * extent specified... but it'll be close).
+ * Default is false.
*
* Returns:
* {Integer} The index of the zoomLevel (entry in the resolutions array)
- * that is the smallest resolution that is greater than the passed-in
- * resolution.
+ * that corresponds to the best fit resolution given the passed in
+ * value and the 'closest' specification.
*/
- getZoomForResolution: function(resolution) {
-
- for(var i=1; i < this.resolutions.length; i++) {
- if ( this.resolutions[i] < resolution) {
- break;
+ getZoomForResolution: function(resolution, closest) {
+ var diff;
+ var minDiff = Number.POSITIVE_INFINITY;
+ for(var i=0; i < this.resolutions.length; i++) {
+ if (closest) {
+ diff = Math.abs(this.resolutions[i] - resolution);
+ if (diff > minDiff) {
+ break;
+ }
+ minDiff = diff;
+ } else {
+ if (this.resolutions[i] < resolution) {
+ break;
+ }
}
}
- return (i - 1);
+ return Math.max(0, i-1);
},
/**
Modified: sandbox/euzuro/zoomToResolution/lib/OpenLayers/Map.js
===================================================================
--- sandbox/euzuro/zoomToResolution/lib/OpenLayers/Map.js 2007-10-03 03:32:17 UTC (rev 4767)
+++ sandbox/euzuro/zoomToResolution/lib/OpenLayers/Map.js 2007-10-03 05:27:48 UTC (rev 4768)
@@ -711,13 +711,13 @@
// simply set center but force zoom change
this.setCenter(
center,
- this.getZoomForResolution(this.resolution),
+ this.getZoomForResolution(this.resolution, true),
false, true
);
} else {
// zoom to oldExtent *and* force zoom change
this.setCenter(oldExtent.getCenterLonLat(),
- this.getZoomForExtent(oldExtent),
+ this.getZoomForExtent(oldExtent, true),
false, true);
}
}
Modified: sandbox/euzuro/zoomToResolution/tests/test_Layer.html
===================================================================
--- sandbox/euzuro/zoomToResolution/tests/test_Layer.html 2007-10-03 03:32:17 UTC (rev 4767)
+++ sandbox/euzuro/zoomToResolution/tests/test_Layer.html 2007-10-03 05:27:48 UTC (rev 4768)
@@ -158,7 +158,7 @@
function test_06_Layer_getZoomForResolution(t) {
- t.plan(4);
+ t.plan(8);
var layer = new OpenLayers.Layer('Test Layer');
@@ -170,6 +170,12 @@
t.eq(layer.getZoomForResolution(3), 5, "zoom allmost all the way in");
t.eq(layer.getZoomForResolution(1), 6, "zoom all the way in");
+ t.eq(layer.getZoomForResolution(65), 0, "smallest containing res");
+ t.eq(layer.getZoomForResolution(63), 1, "smallest containing res");
+
+ t.eq(layer.getZoomForResolution(65, true), 1, "closest res");
+ t.eq(layer.getZoomForResolution(63, true), 1, "closest res");
+
}
function test_07_Layer_redraw(t) {
More information about the Commits
mailing list