[OpenLayers-Commits] r10997 - in trunk/openlayers: lib/OpenLayers/Layer tests/Layer

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Mon Jan 3 13:18:44 EST 2011


Author: ahocevar
Date: 2011-01-03 10:18:43 -0800 (Mon, 03 Jan 2011)
New Revision: 10997

Modified:
   trunk/openlayers/lib/OpenLayers/Layer/Bing.js
   trunk/openlayers/tests/Layer/Bing.html
Log:
fix Bing layer's zoom level management. r=tschaub (closes #2987)


Modified: trunk/openlayers/lib/OpenLayers/Layer/Bing.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Layer/Bing.js	2011-01-03 15:48:37 UTC (rev 10996)
+++ trunk/openlayers/lib/OpenLayers/Layer/Bing.js	2011-01-03 18:18:43 UTC (rev 10997)
@@ -19,35 +19,6 @@
 OpenLayers.Layer.Bing = OpenLayers.Class(OpenLayers.Layer.XYZ, {
 
     /**
-     * Constant: RESOLUTIONS
-     */
-    RESOLUTIONS: [
-        78271.517,
-        39135.7585,
-        19567.87925,
-        9783.939625,
-        4891.9698125,
-        2445.98490625,
-        1222.992453125,
-        611.4962265625,
-        305.74811328125,
-        152.874056640625,
-        76.4370283203125,
-        38.21851416015625,
-        19.109257080078127,
-        9.554628540039063,
-        4.777314270019532,
-        2.388657135009766,
-        1.194328567504883,
-        0.5971642837524415,
-        0.29858214187622073,
-        0.14929107093811037,
-        0.07464553546905518,
-        0.03732276773452759,
-        0.018661383867263796
-    ],
-
-    /**
      * Property: attributionTemplate
      * {String}
      */
@@ -59,12 +30,6 @@
          'Terms of Use</a></span>',
 
     /**
-     * Property: sphericalMercator
-     * {Boolean} always true for this layer type
-     */
-    sphericalMercator: true,
-    
-    /**
      * Property: metadata
      * {Object} Metadata for this layer, as returned by the callback script
      */
@@ -131,7 +96,14 @@
             OpenLayers.Layer.Bing.prototype.EVENT_TYPES.concat(
             OpenLayers.Layer.prototype.EVENT_TYPES
         );
+        
+        options = OpenLayers.Util.applyDefaults({
+            zoomOffset: 1,
+            maxResolution: 78271.51695,
+            sphericalMercator: true
+        }, options)
         var name = options.name || "Bing " + (options.type || this.type);
+        
         var newArgs = [name, null, options];
         OpenLayers.Layer.XYZ.prototype.initialize.apply(this, newArgs);
         this.loadMetadata(this.type); 
@@ -172,15 +144,22 @@
         for (var i=0; i<res.imageUrlSubdomains.length; ++i) {
             this.url.push(url.replace("{subdomain}", res.imageUrlSubdomains[i]));
         };
-        
+        var resolution;
+        if (this.map && this.map.baseLayer === this && this.map.getCenter()) {
+            // if we are the current base layer and the map was centered
+            // already, we need to remember the current resolution to find a
+            // matching zoom level after the maxResolution changes
+            resolution = this.getResolution();
+        }
         this.addOptions({
-            resolutions: this.RESOLUTIONS.slice(res.zoomMin-1, res.zoomMax-1),
-            zoomOffset: res.zoomMin
+            maxResolution: 78271.51695 / Math.pow(2, res.zoomMin - 1),
+            numZoomLevels: res.zoomMax - res.zoomMin + 1
         });
-        if (this.map) {
-            this.redraw();
-            this.updateAttribution();
-        }
+        // adjust zoom level to match the previous resolution - this triggers a
+        // moveTo on all layers.
+        resolution && this.map.zoomTo(this.getZoomForResolution(resolution));
+        // redraw to replace "blank.gif" tiles with real tiles
+        this.redraw();
     },
 
     /**

Modified: trunk/openlayers/tests/Layer/Bing.html
===================================================================
--- trunk/openlayers/tests/Layer/Bing.html	2011-01-03 15:48:37 UTC (rev 10996)
+++ trunk/openlayers/tests/Layer/Bing.html	2011-01-03 18:18:43 UTC (rev 10997)
@@ -2,7 +2,7 @@
 <head>
   <script src="../../lib/OpenLayers.js"></script>
   <script type="text/javascript">
-    var layer; 
+    var map, layer; 
 
     var layerType = 'Aerial';
     var key = "AqTGBsziZHIJYYxgivLBf0hVdrAk9mWO5cQcb8Yux8sW5M8c8opEC2lZqKR1ZZXf";
@@ -30,6 +30,54 @@
         });
     }
     
+    function test_initLayer(t) {
+        t.plan(2);
+
+        var meta = [];
+        var origProcessMetadata = OpenLayers.Layer.Bing.processMetadata;
+        OpenLayers.Layer.Bing.processMetadata = function(metadata) {
+            meta.push(metadata);
+        }
+        map = new OpenLayers.Map("map");
+        layer = new OpenLayers.Layer.Bing(options);
+        var extent;
+        map.addLayers([layer, new OpenLayers.Layer(null, {
+            moveTo: function(bounds, changed) {
+                extent = bounds;
+            }
+        })]);
+        map.zoomToMaxExtent();
+        
+        var map2 = new OpenLayers.Map("map");
+        var layer2 = new OpenLayers.Layer.Bing(OpenLayers.Util.extend({
+            initLayer: function() {
+                // pretend we have a zoomMin of 2
+                this.metadata.resourceSets[0].resources[0].zoomMin = 2;
+                OpenLayers.Layer.Bing.prototype.initLayer.apply(this, arguments);
+            }
+        }, options));
+        var extent2;
+        map2.addLayers([layer2, new OpenLayers.Layer(null, {
+            moveTo: function(bounds, changed) {
+                extent2 = bounds;
+            }
+        })]);
+        map2.zoomToMaxExtent();
+        
+        t.delay_call(2.0, function() {
+            origProcessMetadata.call(layer, meta[0]);
+            t.eq(extent.toBBOX(), map.getExtent().toBBOX(), "layer extent correct for base layer with zoomMin == 1.");
+            map.destroy();
+        });
+
+        t.delay_call(2.5, function() {
+            origProcessMetadata.call(layer2, meta[1]);
+            t.eq(extent2.toBBOX(), map2.getExtent().toBBOX(), "layer extent correct for base layer with zoomMin == 2.");
+            map2.destroy();
+            OpenLayers.Layer.Bing.processMetadata = origProcessMetadata;
+        });
+    }
+    
     function test_attribution(t) {
         t.plan(3);
         



More information about the Commits mailing list