add method OpenLayers.LonLat.fromArray for API-consistency. r=bartvde, p=me (closes #3443)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@12197 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -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 <OpenLayers.LonLat> 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:
|
||||
* {<OpenLayers.LonLat>} New <OpenLayers.LonLat> 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);
|
||||
};
|
||||
|
||||
@@ -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 );
|
||||
|
||||
Reference in New Issue
Block a user