From 399c8ff6432f75801d7f39557679a6308678a311 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Thu, 14 Feb 2013 14:38:35 -0700 Subject: [PATCH] Correct calculation of anchor for zooming Quick explanation: Let targetCenterPx be described by PX and PY. Let oldRes and newRes be R0 and R1 respectively. Let centerPx (center after zoom) be described by CX and CY. And assume there is some anchored pixel point out there that represents the same map location before and after zoom. Let this be the origin OX and OY. We want to recenter the map on the provided box. This means the map distance between the origin and box center at R0 is the same as the map distance between the origin and the map center at R1. That is, R0 * (OX - PX) = R1 * (OX - CX), and R1 * (OY - PY) = R1 * (OY - CY) Or, solving for OX and OY: OX = (R0 * PX - R1 * CX) / (R0 - R1), and OY = (R0 * PY - R1 * CY) / (R0 - R1) --- lib/OpenLayers/Control/ZoomBox.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/OpenLayers/Control/ZoomBox.js b/lib/OpenLayers/Control/ZoomBox.js index 68fd1c3d34..f8cea39bed 100644 --- a/lib/OpenLayers/Control/ZoomBox.js +++ b/lib/OpenLayers/Control/ZoomBox.js @@ -103,10 +103,10 @@ OpenLayers.Control.ZoomBox = OpenLayers.Class(OpenLayers.Control, { oldRes = this.map.getResolution(), newRes = this.map.getResolutionForZoom(zoom), zoomOriginPx = { - x: targetCenterPx.x + - (targetCenterPx.x - centerPx.x) * newRes / oldRes, - y: targetCenterPx.y + - (targetCenterPx.y - centerPx.y) * newRes / oldRes + x: (oldRes * targetCenterPx.x - newRes * centerPx.x) / + (oldRes - newRes), + y: (oldRes * targetCenterPx.y - newRes * centerPx.y) / + (oldRes - newRes) }; this.map.zoomTo(zoom, zoomOriginPx); if (lastZoom == this.map.getZoom() && this.alwaysZoom == true){