Caching bounds center location. r=elemoine (closes #1814)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@8286 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2008-11-05 22:23:26 +00:00
parent d9adcf5a4e
commit b9dac66274
2 changed files with 49 additions and 5 deletions

View File

@@ -38,7 +38,14 @@ OpenLayers.Bounds = OpenLayers.Class({
* Property: top
* {Number} Maximum vertical coordinate.
*/
top: null,
top: null,
/**
* Property: centerLonLat
* {<OpenLayers.LonLat>} A cached center location. This should not be
* accessed directly. Use <getCenterLonLat> instead.
*/
centerLonLat: null,
/**
* Constructor: OpenLayers.Bounds
@@ -215,8 +222,12 @@ OpenLayers.Bounds = OpenLayers.Class({
* {<OpenLayers.LonLat>} 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({
* {<OpenLayers.Bounds>} 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(

View File

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