Allow use of array for bounds.

This commit is contained in:
tschaub
2011-10-26 15:40:40 -06:00
parent 75a1a8e119
commit 98ee7167c4
4 changed files with 52 additions and 3 deletions

View File

@@ -67,6 +67,12 @@ OpenLayers.Bounds = OpenLayers.Class({
* top - {Number} The top bounds.
*/
initialize: function(left, bottom, right, top) {
if (OpenLayers.Util.isArray(left)) {
top = left[3];
right = left[2];
bottom = left[1];
left = left[0];
}
if (left != null) {
this.left = OpenLayers.Util.toFloat(left);
}

View File

@@ -478,6 +478,14 @@ OpenLayers.Map = OpenLayers.Class({
// now override default options
OpenLayers.Util.extend(this, options);
// allow extents to be arrays
if (this.maxExtent && !(this.maxExtent instanceof OpenLayers.Bounds)) {
this.maxExtent = new OpenLayers.Bounds(this.maxExtent);
}
if (this.restrictedExtent && !(this.restrictedExtent instanceof OpenLayers.Bounds)) {
this.restrictedExtent = new OpenLayers.Bounds(this.restrictedExtent);
}
// initialize layers array
this.layers = [];
@@ -2211,6 +2219,9 @@ OpenLayers.Map = OpenLayers.Class({
*
*/
zoomToExtent: function(bounds, closest) {
if (!(bounds instanceof OpenLayers.Bounds)) {
bounds = new OpenLayers.Bounds(bounds);
}
var center = bounds.getCenterLonLat();
if (this.baseLayer.wrapDateLine) {
var maxExtent = this.getMaxExtent();

View File

@@ -4,7 +4,7 @@
<script type="text/javascript">
var bounds;
function test_Bounds_constructor (t) {
t.plan( 21 );
t.plan( 26 );
bounds = new OpenLayers.Bounds();
t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" );
@@ -44,6 +44,15 @@
t.eq( bounds.bottom, -20037508.34, "bounds.bottom adjusted for floating precision");
t.eq( bounds.right, 40075016.68, "bounds.right adjusted for floating precision");
t.eq( bounds.top, 20037508.34, "bounds.top adjusted for floating precision");
// allow construction from a single arg
bounds = new OpenLayers.Bounds([-180, -90, 180, 90]);
t.ok(bounds instanceof OpenLayers.Bounds, "(array) correct instance");
t.eq(bounds.left, -180, "(array) left");
t.eq(bounds.bottom, -90, "(array) bottom");
t.eq(bounds.right, 180, "(array) right");
t.eq(bounds.top, 90, "(array) top");
}
function test_Bounds_constructorFromStrings(t) {

View File

@@ -38,8 +38,10 @@
}
function test_Map_constructor_convenience(t) {
t.plan(3);
t.plan(13);
var map = new OpenLayers.Map({
maxExtent: [-170, -80, 170, 80],
restrictedExtent: [-120, -65, 120, 65],
layers: [
new OpenLayers.Layer(null, {isBaseLayer: true})
],
@@ -47,6 +49,20 @@
zoom: 3
});
// maxExtent from array
t.ok(map.maxExtent instanceof OpenLayers.Bounds, "maxExtent bounds");
t.eq(map.maxExtent.left, -170, "maxExtent left");
t.eq(map.maxExtent.bottom, -80, "maxExtent bottom");
t.eq(map.maxExtent.right, 170, "maxExtent right");
t.eq(map.maxExtent.top, 80, "maxExtent top");
// restrictedExtent from array
t.ok(map.restrictedExtent instanceof OpenLayers.Bounds, "restrictedExtent bounds");
t.eq(map.restrictedExtent.left, -120, "restrictedExtent left");
t.eq(map.restrictedExtent.bottom, -65, "restrictedExtent bottom");
t.eq(map.restrictedExtent.right, 120, "restrictedExtent right");
t.eq(map.restrictedExtent.top, 65, "restrictedExtent top");
var center = map.getCenter();
t.eq(center.lon, -111, "center lon");
t.eq(center.lat, 45, "center lat");
@@ -1412,7 +1428,7 @@
}
function test_Map_zoomToExtent(t) {
t.plan(9);
t.plan(12);
var map = new OpenLayers.Map("map");
var layer = new OpenLayers.Layer(null, {isBaseLayer: true});
@@ -1441,6 +1457,13 @@
t.eq(center.lon, -105, "c) correct x");
t.eq(center.lat, 42, "c) correct y");
t.eq(map.getZoom(), 3, "c) correct zoom");
// accept array
map.zoomToExtent([-160, 15, -50, 69]);
center = map.getCenter();
t.eq(center.lon, -105, "(array) correct x");
t.eq(center.lat, 42, "(array) correct y");
t.eq(map.getZoom(), 2, "(array) correct zoom");
map.destroy();
}