[OpenLayers-Commits] r11425 - sandbox/ahocevar/layercontainer/lib/OpenLayers

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Thu Feb 24 10:52:03 EST 2011


Author: ahocevar
Date: 2011-02-24 07:52:03 -0800 (Thu, 24 Feb 2011)
New Revision: 11425

Modified:
   sandbox/ahocevar/layercontainer/lib/OpenLayers/Layer.js
   sandbox/ahocevar/layercontainer/lib/OpenLayers/Map.js
Log:
switch between centerPx and center, depending on the method we used for positioning/moving the map

Modified: sandbox/ahocevar/layercontainer/lib/OpenLayers/Layer.js
===================================================================
--- sandbox/ahocevar/layercontainer/lib/OpenLayers/Layer.js	2011-02-24 15:48:11 UTC (rev 11424)
+++ sandbox/ahocevar/layercontainer/lib/OpenLayers/Layer.js	2011-02-24 15:52:03 UTC (rev 11425)
@@ -1211,21 +1211,16 @@
     getLonLatFromViewPortPx: function (viewPortPx) {
         var lonlat = null;
         if (viewPortPx != null) {
-            var size = this.map.getSize();
-            var center = this.map.getCenter();
-            if (center) {
-                var res  = this.map.getResolution();
-        
-                var delta_x = viewPortPx.x - (size.w / 2);
-                var delta_y = viewPortPx.y - (size.h / 2);
-            
-                lonlat = new OpenLayers.LonLat(center.lon + delta_x * res ,
-                                             center.lat - delta_y * res); 
+            var map = this.map;
+            var res = map.getResolution();
+            var maxExtent = map.getMaxExtent();
+            var lon = (viewPortPx.x - map.minPx.x) * res + maxExtent.left;
+            var lat = (map.minPx.y - viewPortPx.y) * res + maxExtent.top;
+            lonlat = new OpenLayers.LonLat(lon, lat);
 
-                if (this.wrapDateLine) {
-                    lonlat = lonlat.wrapDateLine(this.maxExtent);
-                }
-            } // else { DEBUG STATEMENT }
+            if (this.wrapDateLine) {
+                lonlat = lonlat.wrapDateLine(this.maxExtent);
+            }
         }
         return lonlat;
     },

Modified: sandbox/ahocevar/layercontainer/lib/OpenLayers/Map.js
===================================================================
--- sandbox/ahocevar/layercontainer/lib/OpenLayers/Map.js	2011-02-24 15:48:11 UTC (rev 11424)
+++ sandbox/ahocevar/layercontainer/lib/OpenLayers/Map.js	2011-02-24 15:52:03 UTC (rev 11425)
@@ -1152,7 +1152,7 @@
             if (OpenLayers.Util.indexOf(this.layers, newBaseLayer) != -1) {
 
                 // preserve center and scale when changing base layers
-                var center = this.getCenter();
+                var center = this.getCachedCenter();
                 var newResolution = OpenLayers.Util.getResolutionFromScale(
                     this.getScale(), newBaseLayer.units
                 );
@@ -1414,7 +1414,7 @@
                     this.layers[i].onMapResize();                
                 }
     
-                var center = this.getCenter();
+                var center = this.getCachedCenter();
     
                 if (this.baseLayer != null && center != null) {
                     var zoom = this.getZoom();
@@ -1465,7 +1465,7 @@
         var extent = null;
         
         if (center == null) {
-            center = this.getCenter();
+            center = this.getCachedCenter();
         }                
         if (resolution == null) {
             resolution = this.getResolution();
@@ -1505,23 +1505,24 @@
      */
     getCenter: function () {
         var center = null;
-        if (this.center) {
-            center = this.center.clone();
+        var cachedCenter = this.getCachedCenter();
+        if (cachedCenter) {
+            center = cachedCenter.clone();
         }
         return center;
     },
 
     /**
-     * Method: getCachedCenterPx
+     * Method: getCachedCenter
      *
      * Returns:
-     * {<OpenLayers.Pixel>}
+     * {<OpenLayers.LonLat>}
      */
-    getCachedCenterPx: function() {
-        if (!this.centerPx) {
-            this.centerPx = this.getViewPortPxFromLonLat(this.getCenter());
+    getCachedCenter: function() {
+        if (!this.center) {
+            this.center = this.getLonLatFromViewPortPx(this.centerPx);
         }
-        return this.centerPx;
+        return this.center;
     },
 
     /**
@@ -1553,11 +1554,14 @@
         });
         if (options.dragging) {
             if (dx != 0 || dy != 0) {
+                if (!this.centerPx) {
+                    this.centerPx = this.getViewPortPxFromLonLat(this.center);
+                }
                 this.moveByPx(dx, dy);
             }
         } else {
             // getCenter
-            var centerPx = this.getCachedCenterPx();
+            var centerPx = this.getViewPortPxFromLonLat(this.getCachedCenter());
 
             // adjust
             var newCenterPx = centerPx.add(dx, dy);
@@ -1570,6 +1574,7 @@
                     this.setCenter(newCenterLonLat, null, options.dragging);
                 }    
             }
+            this.centerPx = null;
         }        
 
    },
@@ -1587,7 +1592,7 @@
             if (!this.panTween) {
                 this.panTween = new OpenLayers.Tween(this.panMethod);
             }
-            var center = this.getCenter();
+            var center = this.getCachedCenter();
 
             // center will not change, don't do nothing
             if (lonlat.lon == center.lon &&
@@ -1660,13 +1665,14 @@
      * dy - {Number}
      */
     moveByPx: function(dx, dy) {
-        var centerPx = this.getCachedCenterPx().add(dx, dy);
+        var centerPx = this.centerPx.add(dx, dy);
         var valid = centerPx.x <= this.maxPx.x &&
                     centerPx.x >= this.minPx.x &&
                     centerPx.y <= this.maxPx.y &&
                     centerPx.y >= this.minPx.y;
         if (valid === true) {
             this.center = null;
+            this.centerPx = centerPx;
             if (dx) {
                 this.layerContainerDiv.style.left =
                     parseInt(this.layerContainerDiv.style.left) - dx + "px";
@@ -1722,14 +1728,14 @@
             this.panTween.stop();
         }    
              
-        if (!this.center && !this.isValidLonLat(lonlat)) {
+        if (!this.getCachedCenter() && !this.isValidLonLat(lonlat)) {
             lonlat = this.maxExtent.getCenterLonLat();
         }
 
         if(this.restrictedExtent != null) {
             // In 3.0, decide if we want to change interpretation of maxExtent.
             if(lonlat == null) { 
-                lonlat = this.getCenter(); 
+                lonlat = this.getCachedCenter(); 
             }
             if(zoom == null) { 
                 zoom = this.getZoom(); 
@@ -1765,7 +1771,7 @@
                             (zoom != this.getZoom()) );
 
         var centerChanged = (this.isValidLonLat(lonlat)) && 
-                            (!lonlat.equals(this.center));
+                            (!lonlat.equals(this.getCachedCenter()));
 
 
         // if neither center nor zoom will change, no need to do anything
@@ -1776,24 +1782,26 @@
             }
 
             if (centerChanged) {
-                if ((!zoomChanged) && (this.center)) { 
+                if ((!zoomChanged) && (this.getCachedCenter())) { 
                     // if zoom hasnt changed, just slide layerContainer
                     //  (must be done before setting this.center to new value)
                     this.centerLayerContainer(lonlat);
                 }
                 this.center = lonlat.clone();
+                this.centerPx = null;
             }
 
             // (re)set the layerContainerDiv's location
             if ((zoomChanged) || (this.layerContainerOrigin == null)) {
-                this.layerContainerOrigin = this.center.clone();
+                this.layerContainerOrigin = this.getCachedCenter();
                 this.layerContainerDiv.style.left = "0px";
                 this.layerContainerDiv.style.top  = "0px";
                 var res = this.resolution || this.getResolutionForZoom(zoom);
                 var maxExtent = this.getMaxExtent();
                 var maxExtentCenter = maxExtent.getCenterLonLat();
-                var lonDelta = this.center.lon - maxExtentCenter.lon;
-                var latDelta = maxExtentCenter.lat - this.center.lat;
+                var cachedCenter = this.getCachedCenter();
+                var lonDelta = cachedCenter.lon - maxExtentCenter.lon;
+                var latDelta = maxExtentCenter.lat - cachedCenter.lat;
                 var extentWidth = Math.round(maxExtent.getWidth() / res);
                 var extentHeight = Math.round(maxExtent.getHeight() / res);
                 var left = (this.size.w - extentWidth) / 2 - lonDelta / res;
@@ -1886,7 +1894,6 @@
      * lonlat - {<OpenLayers.LonLat>}
      */
     centerLayerContainer: function (lonlat) {
-
         var originPx = this.getViewPortPxFromLonLat(this.layerContainerOrigin);
         var newPx = this.getViewPortPxFromLonLat(lonlat);
 
@@ -1903,8 +1910,8 @@
             this.maxPx.x -= dx;
             this.minPx.y -= dy;
             this.maxPx.y -= dy;
-        }
-        
+            this.centerPx = this.getViewPortPxFromLonLat(lonlat);
+        }        
     },
 
     /**
@@ -2318,7 +2325,7 @@
         var size = this.getSize();
         var w_deg = size.w * res;
         var h_deg = size.h * res;
-        var center = this.getCenter();
+        var center = this.getCachedCenter();
 
         var extent = new OpenLayers.Bounds(center.lon - w_deg / 2,
                                            center.lat - h_deg / 2,
@@ -2430,8 +2437,8 @@
      * {<OpenLayers.Size>} The geodesic size of the pixel in kilometers.
      */
     getGeodesicPixelSize: function(px) {
-        var lonlat = px ? this.getLonLatFromPixel(px) : (this.getCenter() ||
-            new OpenLayers.LonLat(0, 0));
+        var lonlat = px ? this.getLonLatFromPixel(px) : (
+            this.getCachedCenter() || new OpenLayers.LonLat(0, 0));
         var res = this.getResolution();
         var left = lonlat.add(-res / 2, 0);
         var right = lonlat.add(res / 2, 0);



More information about the Commits mailing list