From 75a1a8e119b2948bc57340daad231dbb6cba364d Mon Sep 17 00:00:00 2001 From: tschaub Date: Wed, 26 Oct 2011 15:22:13 -0600 Subject: [PATCH 1/7] Allow setting center from array. --- lib/OpenLayers/BaseTypes/LonLat.js | 4 ++++ lib/OpenLayers/Map.js | 3 +++ tests/BaseTypes/LonLat.html | 8 +++++++- tests/Map.html | 30 ++++++++++++++++++++++++++++-- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/lib/OpenLayers/BaseTypes/LonLat.js b/lib/OpenLayers/BaseTypes/LonLat.js index 70c883d4f1..77206f4217 100644 --- a/lib/OpenLayers/BaseTypes/LonLat.js +++ b/lib/OpenLayers/BaseTypes/LonLat.js @@ -38,6 +38,10 @@ OpenLayers.LonLat = OpenLayers.Class({ * it will be the y coordinate of the map location in your map units. */ initialize: function(lon, lat) { + if (OpenLayers.Util.isArray(lon)) { + lat = lon[1]; + lon = lon[0]; + } this.lon = OpenLayers.Util.toFloat(lon); this.lat = OpenLayers.Util.toFloat(lat); }, diff --git a/lib/OpenLayers/Map.js b/lib/OpenLayers/Map.js index 8ce24e374d..90ead46cd4 100644 --- a/lib/OpenLayers/Map.js +++ b/lib/OpenLayers/Map.js @@ -1703,6 +1703,9 @@ OpenLayers.Map = OpenLayers.Class({ * options - {Object} */ moveTo: function(lonlat, zoom, options) { + if (!(lonlat instanceof OpenLayers.LonLat)) { + lonlat = new OpenLayers.LonLat(lonlat); + } if (!options) { options = {}; } diff --git a/tests/BaseTypes/LonLat.html b/tests/BaseTypes/LonLat.html index 3135a41ae3..d5283cf286 100644 --- a/tests/BaseTypes/LonLat.html +++ b/tests/BaseTypes/LonLat.html @@ -6,7 +6,7 @@ var lonlat; function test_LonLat_constructor (t) { - t.plan( 6 ); + t.plan( 8 ); lonlat = new OpenLayers.LonLat(6, 5); t.ok( lonlat instanceof OpenLayers.LonLat, "new OpenLayers.LonLat returns LonLat object" ); t.eq( lonlat.CLASS_NAME, "OpenLayers.LonLat", "lonlat.CLASS_NAME is set correctly"); @@ -17,6 +17,12 @@ lonlat = new OpenLayers.LonLat(20037508.33999999, -20037508.33999999); t.eq( lonlat.lon, 20037508.34, "lonlat.lon rounds correctly"); t.eq( lonlat.lat, -20037508.34, "lonlat.lat rounds correctly"); + + // allow construction from single arg + lonlat = new OpenLayers.LonLat([1, 2]); + t.eq(lonlat.lon, 1, "lon from array"); + t.eq(lonlat.lat, 2, "lat from array"); + } function test_LonLat_constructorFromStrings (t) { diff --git a/tests/Map.html b/tests/Map.html index 4e93f104b6..3476ab959c 100644 --- a/tests/Map.html +++ b/tests/Map.html @@ -37,6 +37,25 @@ map.destroy(); } + function test_Map_constructor_convenience(t) { + t.plan(3); + var map = new OpenLayers.Map({ + layers: [ + new OpenLayers.Layer(null, {isBaseLayer: true}) + ], + center: [-111, 45], + zoom: 3 + }); + + var center = map.getCenter(); + t.eq(center.lon, -111, "center lon"); + t.eq(center.lat, 45, "center lat"); + + t.eq(map.getZoom(), 3, "zoom"); + + map.destroy(); + } + function test_Map_constructor_late_rendering(t) { t.plan( 4 ); @@ -163,7 +182,7 @@ } function test_Map_center(t) { - t.plan(11); + t.plan(14); var log = []; map = new OpenLayers.Map('map', { eventListeners: { @@ -186,7 +205,7 @@ t.ok( map.getCenter().equals(ll), "map center is correct after calling setCenter, zoom in"); map.zoomOut(); t.eq( map.getZoom(), 0, "map.zoom is correct after calling setCenter,zoom in, zoom out"); - + log = []; map.zoomTo(5); t.eq(log[0], "movestart", "zoomTo fires movestart event"); @@ -205,6 +224,13 @@ map.getCenter().lon = 10; t.ok( map.getCenter().equals(ll), "map.getCenter returns a clone of map.center"); + // allow calling setCenter with an array + map.setCenter([4, 2]); + var center = map.getCenter(); + t.ok(center instanceof OpenLayers.LonLat, "(array) center is lonlat"); + t.eq(center.lon, 4, "(array) center lon"); + t.eq(center.lat, 2, "(array) center lat"); + map.destroy(); } From 98ee7167c43cc828429c5d407b67b6993fea3554 Mon Sep 17 00:00:00 2001 From: tschaub Date: Wed, 26 Oct 2011 15:40:40 -0600 Subject: [PATCH 2/7] Allow use of array for bounds. --- lib/OpenLayers/BaseTypes/Bounds.js | 6 ++++++ lib/OpenLayers/Map.js | 11 +++++++++++ tests/BaseTypes/Bounds.html | 11 ++++++++++- tests/Map.html | 27 +++++++++++++++++++++++++-- 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/lib/OpenLayers/BaseTypes/Bounds.js b/lib/OpenLayers/BaseTypes/Bounds.js index 5935ccbe1a..9eefcc1831 100644 --- a/lib/OpenLayers/BaseTypes/Bounds.js +++ b/lib/OpenLayers/BaseTypes/Bounds.js @@ -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); } diff --git a/lib/OpenLayers/Map.js b/lib/OpenLayers/Map.js index 90ead46cd4..33daa3a66c 100644 --- a/lib/OpenLayers/Map.js +++ b/lib/OpenLayers/Map.js @@ -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(); diff --git a/tests/BaseTypes/Bounds.html b/tests/BaseTypes/Bounds.html index b436005d1c..b8ff57eceb 100644 --- a/tests/BaseTypes/Bounds.html +++ b/tests/BaseTypes/Bounds.html @@ -4,7 +4,7 @@