Allow setting center from array.

This commit is contained in:
tschaub
2011-10-26 15:22:13 -06:00
parent 415c8f9e0c
commit 75a1a8e119
4 changed files with 42 additions and 3 deletions

View File

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

View File

@@ -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 = {};
}

View File

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

View File

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