From b9dac66274f52aa1824b89d35ecf98e33e8ce68d Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Wed, 5 Nov 2008 22:23:26 +0000 Subject: [PATCH] Caching bounds center location. r=elemoine (closes #1814) git-svn-id: http://svn.openlayers.org/trunk/openlayers@8286 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/BaseTypes/Bounds.js | 25 ++++++++++++++++++++----- tests/BaseTypes/Bounds.html | 29 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/lib/OpenLayers/BaseTypes/Bounds.js b/lib/OpenLayers/BaseTypes/Bounds.js index e79454f9d1..e416bdc5cf 100644 --- a/lib/OpenLayers/BaseTypes/Bounds.js +++ b/lib/OpenLayers/BaseTypes/Bounds.js @@ -38,7 +38,14 @@ OpenLayers.Bounds = OpenLayers.Class({ * Property: top * {Number} Maximum vertical coordinate. */ - top: null, + top: null, + + /** + * Property: centerLonLat + * {} A cached center location. This should not be + * accessed directly. Use instead. + */ + centerLonLat: null, /** * Constructor: OpenLayers.Bounds @@ -215,8 +222,12 @@ OpenLayers.Bounds = OpenLayers.Class({ * {} The center of the bounds in map space. */ getCenterLonLat:function() { - return new OpenLayers.LonLat( (this.left + this.right) / 2, - (this.bottom + this.top) / 2); + if(!this.centerLonLat) { + this.centerLonLat = new OpenLayers.LonLat( + (this.left + this.right) / 2, (this.bottom + this.top) / 2 + ); + } + return this.centerLonLat; }, /** @@ -257,7 +268,7 @@ OpenLayers.Bounds = OpenLayers.Class({ var bottom = (this.bottom - origy) * ratio + origy; var right = (this.right - origx) * ratio + origx; var top = (this.top - origy) * ratio + origy; - + return new OpenLayers.Bounds(left, bottom, right, top); }, @@ -291,8 +302,9 @@ OpenLayers.Bounds = OpenLayers.Class({ * object - {Object} Can be LonLat, Point, or Bounds */ extend:function(object) { - var bounds = null; if (object) { + // clear cached center location + this.centerLonLat = null; switch(object.CLASS_NAME) { case "OpenLayers.LonLat": bounds = new OpenLayers.Bounds(object.lon, object.lat, @@ -309,6 +321,7 @@ OpenLayers.Bounds = OpenLayers.Class({ } if (bounds) { + var bounds = null; if ( (this.left == null) || (bounds.left < this.left)) { this.left = bounds.left; } @@ -499,6 +512,8 @@ OpenLayers.Bounds = OpenLayers.Class({ * {} Itself, for use in chaining operations. */ transform: function(source, dest) { + // clear cached center location + this.centerLonLat = null; var ll = OpenLayers.Projection.transform( {'x': this.left, 'y': this.bottom}, source, dest); var lr = OpenLayers.Projection.transform( diff --git a/tests/BaseTypes/Bounds.html b/tests/BaseTypes/Bounds.html index 55c21874ed..d49cad9a2d 100644 --- a/tests/BaseTypes/Bounds.html +++ b/tests/BaseTypes/Bounds.html @@ -304,6 +304,35 @@ t.ok( bounds.getCenterPixel().equals(new OpenLayers.Pixel(50, 70)), "getCenterPixel() works correctly"); t.ok( bounds.getCenterLonLat().equals(new OpenLayers.LonLat(50, 70)), "getCenterLonLat() works correctly"); } + + function test_getCenterLonLat(t) { + t.plan(7); + var bounds = new OpenLayers.Bounds(0, 10, 20, 60); + + // set private centerLonLat to confirm that it is getting returned if set + bounds.centerLonLat = "foo"; + t.eq(bounds.getCenterLonLat(), "foo", "returns cached value"); + bounds.centerLonLat = null; + + // unmodified + var center = bounds.getCenterLonLat(); + t.eq(center.lon, 10, "unmodified: correct x"); + t.eq(center.lat, 35, "unmodified: correct y"); + + // transformed + bounds.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")); + center = bounds.getCenterLonLat(); + t.eq(Math.round(center.lon), 1113195, "transformed: correct x"); + t.eq(Math.round(center.lat), 4759314, "transformed: correct y"); + + // extended + bounds.extend(new OpenLayers.Bounds(-10000000, -10000000, 10000000, 10000000)); + center = bounds.getCenterLonLat(); + t.eq(center.lon, 0, "extended: correct x"); + t.eq(center.lat, 0, "extended: correct y"); + + + } function test_Bounds_fromArray(t) { t.plan( 5 );