From 81577d6dce7c9fb2f8124f8337a87b891bee1239 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Sun, 24 Jun 2012 10:09:24 +0200 Subject: [PATCH] Adding Loc.add method. --- src/ol/Loc.js | 16 ++++++++++++++++ src/ol/Map.js | 7 +++---- src/ol/renderer/MapRenderer.js | 7 +++---- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/ol/Loc.js b/src/ol/Loc.js index bee33e636f..035ffa933a 100644 --- a/src/ol/Loc.js +++ b/src/ol/Loc.js @@ -120,6 +120,22 @@ ol.Loc.prototype.doTransform = function(proj) { return new ol.Loc(point['x'], point['y'], this.z_, proj); }; +/** + * Adds the passed x, y(, z) delta to a new location. + * + * @param {number} x + * @param {number} y + * @param {number=} opt_z + * @returns {ol.Loc} + */ +ol.Loc.prototype.add = function(x, y, opt_z) { + var newZ; + if (goog.isDef(this.z_)) { + newZ = (opt_z || 0) + this.z_; + } + return new ol.Loc(this.x_ + x, this.y_ + y, newZ, this.projection_); +}; + /** * Clean up. * @export diff --git a/src/ol/Map.js b/src/ol/Map.js index 613915d1df..02defe01fa 100644 --- a/src/ol/Map.js +++ b/src/ol/Map.js @@ -327,10 +327,9 @@ ol.Map.prototype.setZoom = function(zoom, opt_anchor) { var size = this.getSize(), anchorLoc = this.getLocForPixel(opt_anchor), newRes = this.getResolutionForZoom(newZoom); - newCenter = new ol.Loc( - anchorLoc.getX() + (size.width/2 - opt_anchor.x) * newRes, - anchorLoc.getY() - (size.height/2 - opt_anchor.y) * newRes, - undefined, this.getProjection() + newCenter = anchorLoc.add( + (size.width/2 - opt_anchor.x) * newRes, + (opt_anchor.y - size.height/2) * newRes ); } else { newCenter = this.center_; diff --git a/src/ol/renderer/MapRenderer.js b/src/ol/renderer/MapRenderer.js index 30b3cdbde8..5b4ccee091 100644 --- a/src/ol/renderer/MapRenderer.js +++ b/src/ol/renderer/MapRenderer.js @@ -89,10 +89,9 @@ ol.renderer.MapRenderer.prototype.getLocForPixel = function(pixel) { var center = this.renderedCenter_, resolution = this.renderedResolution_, size = goog.style.getSize(this.container_); - return new ol.Loc( - center.getX() - (size.width/2 - pixel.x)*resolution, - center.getY() + (size.height/2 - pixel.y)*resolution, - undefined, this.renderedCenter_.getProjection() + return center.add( + (pixel.x - size.width/2) * resolution, + (size.height/2 - pixel.y) * resolution ); };