diff --git a/lib/OpenLayers/BaseTypes/LonLat.js b/lib/OpenLayers/BaseTypes/LonLat.js index 21c71927f8..798cd325f4 100644 --- a/lib/OpenLayers/BaseTypes/LonLat.js +++ b/lib/OpenLayers/BaseTypes/LonLat.js @@ -190,3 +190,22 @@ OpenLayers.LonLat.fromString = function(str) { var pair = str.split(","); return new OpenLayers.LonLat(pair[0], pair[1]); }; + +/** + * Function: fromArray + * Alternative constructor that builds a new from an + * array of two numbers that represent lon- and lat-values. + * + * Parameters: + * arr - {Array(Float)} Array of lon/lat values (e.g. [5,-42]) + * + * Returns: + * {} New object built from the + * passed-in array. + */ +OpenLayers.LonLat.fromArray = function(arr) { + var gotArr = OpenLayers.Util.isArray(arr), + lon = gotArr && arr[0], + lat = gotArr && arr[1]; + return new OpenLayers.LonLat(lon, lat); +}; diff --git a/tests/BaseTypes/LonLat.html b/tests/BaseTypes/LonLat.html index c9d790fb7c..009eafec91 100644 --- a/tests/BaseTypes/LonLat.html +++ b/tests/BaseTypes/LonLat.html @@ -112,6 +112,52 @@ var ll = new OpenLayers.LonLat(6, 5); t.ok( lonlat.equals(ll), "lonlat is set correctly"); } + + function test_LonLat_fromArray(t) { + t.plan( 3 ); + + // (1 test) must return a OpenLayers.LonLat-instance + lonlat = OpenLayers.LonLat.fromArray([6,5]); + t.ok( lonlat instanceof OpenLayers.LonLat, "OpenLayers.LonLat.fromArray returns LonLat object" ); + + var ll = new OpenLayers.LonLat(6, 5); + // (1 test) must return correct LonLat-object + t.ok( lonlat.equals(ll), "lonlat is set correctly"); + + + // (1 test) check how function deals with illegal arguments, it should + // never throw an exception and always return an instance of + // OpenLayers.LonLat. + var unexpectedResult = false, + undef, + checkArgs = [ + {}, + '', + 6, + false, + true, + [undef, 5], + [6, undef] + ], + returnedVal; + + try { + for(var i = 0, len = checkArgs.length; i < len; i++ ){ + returnedVal = OpenLayers.LonLat.fromArray( checkArgs[i] ); + if (!(returnedVal instanceof OpenLayers.LonLat) ) { + unexpectedResult = true; + break; + } + } + // no arguments at all + returnedVal = OpenLayers.LonLat.fromArray(); + unexpectedResult = !(returnedVal instanceof OpenLayers.LonLat); + } catch(e) { + unexpectedResult = true; + } finally { + t.ok(!unexpectedResult, "OpenLayers.LonLat.fromArray always returns an instance of OpenLayers.LonLat and doesn't throw an exception when called with unexpected argument."); + } + } function test_LonLat_transform(t) { t.plan( 6 );