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

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Thu Feb 24 18:04:49 EST 2011


Author: ahocevar
Date: 2011-02-24 15:04:49 -0800 (Thu, 24 Feb 2011)
New Revision: 11444

Modified:
   sandbox/ahocevar/layercontainer/lib/OpenLayers/Map.js
Log:
Simplifying things: centerPx is a constant, so no need to track or even change it (in fact, that was the reason for center and centerPx getting out of sync from dragging

Modified: sandbox/ahocevar/layercontainer/lib/OpenLayers/Map.js
===================================================================
--- sandbox/ahocevar/layercontainer/lib/OpenLayers/Map.js	2011-02-24 22:39:40 UTC (rev 11443)
+++ sandbox/ahocevar/layercontainer/lib/OpenLayers/Map.js	2011-02-24 23:04:49 UTC (rev 11444)
@@ -1520,7 +1520,9 @@
      */
     getCachedCenter: function() {
         if (!this.center) {
-            this.center = this.getLonLatFromViewPortPx(this.centerPx);
+            this.center = this.getLonLatFromViewPortPx(
+                new OpenLayers.Pixel(map.size.w / 2, map.size.h / 2)
+            );
         }
         return this.center;
     },
@@ -1554,19 +1556,19 @@
         });
         if (options.dragging) {
             if (dx != 0 || dy != 0) {
-                if (!this.centerPx) {
-                    this.centerPx = this.getViewPortPxFromLonLat(this.center);
-                }
                 this.moveByPx(dx, dy);
             }
         } else {
+            // if we don't have a center, we were using moveByPx previously
+            var forceSetCenter = !this.center;
+            
             // getCenter
             var centerPx = this.getViewPortPxFromLonLat(this.getCachedCenter());
 
             // adjust
             var newCenterPx = centerPx.add(dx, dy);
 
-            if (this.centerPx || !newCenterPx.equals(centerPx)) {
+            if (forceSetCenter || !newCenterPx.equals(centerPx)) {
                 var newCenterLonLat = this.getLonLatFromViewPortPx(newCenterPx);
                 if (options.animate) {
                     this.panTo(newCenterLonLat);
@@ -1574,7 +1576,6 @@
                     this.setCenter(newCenterLonLat, null, options.dragging);
                 }    
             }
-            this.centerPx = null;
         }        
 
    },
@@ -1665,14 +1666,14 @@
      * dy - {Number}
      */
     moveByPx: function(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;
+        var x = this.size.w / 2 + dx;
+        var y = this.size.h / 2 + dy;
+        var valid = x <= this.maxPx.x &&
+                    x >= this.minPx.x &&
+                    y <= this.maxPx.y &&
+                    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";
@@ -1735,7 +1736,7 @@
         if(this.restrictedExtent != null) {
             // In 3.0, decide if we want to change interpretation of maxExtent.
             if(lonlat == null) { 
-                lonlat = this.getCachedCenter(); 
+                lonlat = this.center; 
             }
             if(zoom == null) { 
                 zoom = this.getZoom(); 
@@ -1771,7 +1772,7 @@
                             (zoom != this.getZoom()) );
 
         var centerChanged = (this.isValidLonLat(lonlat)) && 
-                            (!lonlat.equals(this.getCachedCenter()));
+                            (!lonlat.equals(this.center));
 
 
         // if neither center nor zoom will change, no need to do anything
@@ -1782,26 +1783,24 @@
             }
 
             if (centerChanged) {
-                if ((!zoomChanged) && (this.getCachedCenter())) { 
+                if (!zoomChanged && this.center) { 
                     // 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;
             }
 
             var res = this.getResolutionForZoom(zoom);
             // (re)set the layerContainerDiv's location
-            if ((zoomChanged) || (this.layerContainerOrigin == null)) {
+            if (zoomChanged || this.layerContainerOrigin == null) {
                 this.layerContainerOrigin = this.getCachedCenter();
                 this.layerContainerDiv.style.left = "0px";
                 this.layerContainerDiv.style.top  = "0px";
                 var maxExtent = this.getMaxExtent();
                 var maxExtentCenter = maxExtent.getCenterLonLat();
-                var cachedCenter = this.getCachedCenter();
-                var lonDelta = cachedCenter.lon - maxExtentCenter.lon;
-                var latDelta = maxExtentCenter.lat - cachedCenter.lat;
+                var lonDelta = this.center.lon - maxExtentCenter.lon;
+                var latDelta = maxExtentCenter.lat - this.center.lat;
                 var extentWidth = Math.round(maxExtent.getWidth() / res);
                 var extentHeight = Math.round(maxExtent.getHeight() / res);
                 var left = (this.size.w - extentWidth) / 2 - lonDelta / res;



More information about the Commits mailing list