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)
This commit is contained in:
Tim Schaub
2013-02-14 14:38:35 -07:00
parent 1ac16835f2
commit 399c8ff643

View File

@@ -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){