var result = ZGeoTestFunc();
alert(result);
exit();
//------------------------------------
//ZGeoTestFunc()
//------------------------------------
function ZGeoTestFunc ()
{
var retvar = '';
var isandroid = (global('%SDK') != '0');
//test points
var p1 = '40.748785, -73.984902'; //Empire State Building
var p2 = '40.742747, -73.972632'; //123 degree and 1234 m away
var tsec = 1003.252; //time in seconds for speed calculation
retvar += '----------' + '\n';
retvar += 'ZGeoDistance: ' + ZGeoDistance(p1, p2) + '\n'; //1234
retvar += 'ZGeoBearing: ' + ZGeoBearing(p1, p2, 0) + '\n'; //123.00103598235853
retvar += '----------' + '\n';
retvar += 'ZGeoSpeed: ' + ZGeoSpeed(p1, p2, tsec, 2) + '\n'; //1,23
retvar += 'ZGeoCompassDir: ' + ZGeoCompassDir(ZGeoBearing(p1, p2, 0)) + '\n'; //ESE
retvar += '----------' + '\n';
return retvar;
}
//\ZGeoTestFunc ------------------------------------
//------------------------------------
//ZGeoDistance (pointa, pointb, dp)
//Returns the distance between point a and point b in m
//(using Haversine formula)
//Params:
{string} pointa,pointb: comma-separated strings of Latitude,Longitude
of points (eg '40.748785, -73.984902','40.742747, -73.972632');
// {number} dp: number of decimal places to use in the returned distance
//Return: {number} Distance in m between point a and point b
//------------------------------------
function ZGeoDistance (pointa, pointb)
{
var earthradius = 6378137; //Radius of the earth in m, 6378.137 in Km, 3963.191 in Miles, 6975215 in Yards
var d2r = Math.PI / 180.0; //degree to radians , (x * d2r) = toRad(x)
var reterr = '-';
if (typeof pointa == 'undefined') {return reterr;}
if (typeof pointb == 'undefined') {return reterr;}
//trim whitespace and separate Latitude, Longitude
var pa = pointa.replace(/\s/g, "").split(','); //split to pa[0] = Lat and pa[1] = Lon
var pb = pointb.replace(/\s/g, "").split(','); //split to pb[0] = Lat and pb[1] = Lon
//simple check of parameters
if (pa.length != 2 || pb.length != 2 || isNaN(pa[0]) || isNaN(pa[1]) || isNaN(pb[0]) || isNaN(pb[1])) {return reterr;}
dp = typeof(dp) == 'number' ? dp : typeof(dp) == 'string' && dp.replace(/^\s+|\s+$/g, '') != '' ? +dp : NaN;
if (isNaN(dp) || dp < 0 || dp > 10) {dp = 0;}
//calculate distance between point a and point b
var lat1 = pa[0] * d2r, lon1 = pa[1] * d2r;
var lat2 = pb[0] * d2r, lon2 = pb[1] * d2r;
var dLat = lat2 - lat1;
var dLon = lon2 - lon1;
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1) * Math.cos(lat2) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var dist = earthradius * c;
return dist.toFixed(dp);
}
//\ZGeoDistance ------------------------------------
//------------------------------------
//ZGeoBearing (pointa, pointb, dp)
//Returns the (initial) bearing from point a to the point b in degrees
//see
http://williams.best.vwh.net/avform.htm#Crs//Params:
{string} pointa,pointb: comma-separated strings of Latitude,Longitude
of points (eg '40.748785, -73.984902','40.742747, -73.972632');
// {number} dp: number of decimal places to use in the returned degree
//Return: {number} Initial bearing in degrees from North
//------------------------------------
function ZGeoBearing (pointa, pointb, dp)
{
var d2r = Math.PI / 180.0; //degree to radians , (x * d2r) = toRad(x)
var r2d = 180.0 / Math.PI; //radians to degree , (x * r2d) = toDeg(x)
var reterr = '-';
if (typeof pointa == 'undefined') {return reterr;}
if (typeof pointb == 'undefined') {return reterr;}
//trim whitespace and separate Latitude, Longitude
var pa = pointa.replace(/\s/g, "").split(','); //split to pa[0] = Lat and pa[1] = Lon
var pb = pointb.replace(/\s/g, "").split(','); //split to pb[0] = Lat and pb[1] = Lon
//simple check of parameters
if (pa.length != 2 || pb.length != 2 || isNaN(pa[0]) || isNaN(pa[1]) || isNaN(pb[0]) || isNaN(pb[1])) {return reterr;}
dp = typeof(dp) == 'number' ? dp : typeof(dp) == 'string' && dp.replace(/^\s+|\s+$/g, '') != '' ? +dp : NaN;
if (isNaN(dp) || dp < 0 || dp > 10) {dp = 10;}
//calculate bearing from pointa to point b
var lat1 = pa[0] * d2r, lat2 = pb[0] * d2r;
var dLon = (pb[1] - pa[1]) * d2r;
var y = Math.sin(dLon) * Math.cos(lat2);
var x = Math.cos(lat1) * Math.sin(lat2) -
Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
var retvar = (Math.atan2(y, x) * r2d + 360) % 360;
return retvar.toFixed(dp);
}
//\ZGeoBearing ------------------------------------
//------------------------------------
//ZGeoSpeed (pointa, pointb, timesec, dp)
//Returns the speed between point a and point b in m/sec
//Params:
{string} pointa,pointb: comma-separated strings of Latitude,Longitude
of points (eg '40.748785, -73.984902','40.742747, -73.972632');
// {number} timesec: time in seconds between point a and point b
// {number} dp: number of decimal places to use in the returned speed (default: 2)
//Return: {number} speed in m/sec between point a and point b
//------------------------------------
function ZGeoSpeed (pointa, pointb, timesec, dp)
{
var earthradius = 6378137; //Radius of the earth in m, 6378.137 in Km, 3963.191 in Miles, 6975215 in Yards
var d2r = Math.PI / 180.0; //degree to radians , (x * d2r) = toRad(x)
var reterr = '-';
if (typeof pointa == 'undefined') {return reterr;}
if (typeof pointb == 'undefined') {return reterr;}
//trim whitespace and separate Latitude, Longitude
var pa = pointa.replace(/\s/g, "").split(','); //split to pa[0] = Lat and pa[1] = Lon
var pb = pointb.replace(/\s/g, "").split(','); //split to pb[0] = Lat and pb[1] = Lon
//simple check of parameters
if (pa.length != 2 || pb.length != 2 || isNaN(pa[0]) || isNaN(pa[1]) || isNaN(pb[0]) || isNaN(pb[1])) {return reterr;}
timesec = typeof(timesec) == 'number' ? timesec : typeof(timesec) ==
'string' && timesec.replace(/^\s+|\s+$/g, '') != '' ? +timesec :
NaN;
if (isNaN(timesec) || timesec <= 0) {return reterr;}
dp = typeof(dp) == 'number' ? dp : typeof(dp) == 'string' && dp.replace(/^\s+|\s+$/g, '') != '' ? +dp : NaN;
if (isNaN(dp) || dp < 0 || dp > 10) {dp = 2;}
//calculate distance between point a and point b
var lat1 = pa[0] * d2r, lon1 = pa[1] * d2r;
var lat2 = pb[0] * d2r, lon2 = pb[1] * d2r;
var dLat = lat2 - lat1;
var dLon = lon2 - lon1;
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1) * Math.cos(lat2) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var dist = earthradius * c;
return (dist / timesec).toFixed(dp);
}
//\ZGeoSpeed ------------------------------------
//------------------------------------
//ZGeoCompassDir (deg, precision)
//Returns the compass directions from a given degree , e.g. N or NW or NNW ...
//Params: {number} deg: bearing in degrees from North
// {number} precision: [default] (N,NNE,NE,ENE,E,ESE,SE,SSE,S,SSW,SW,WSW,W,WNW,NW,NNW), 2 (N,NE,E,SE,S,SW,W,NW), 1 (N,E,S,W)
//Return: {string} compass direction
//------------------------------------
function ZGeoCompassDir (deg, precision)
{
var reterr = '-';
deg = typeof deg == 'number' ? deg : typeof deg == 'string' && deg.replace(/\s/g, "") != '' ? +deg : NaN;
if (isNaN(deg)) {return reterr;}
deg = parseFloat(deg);
var aDir = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N'];
precision = typeof precision == 'number' ? precision : typeof precision
== 'string' && precision.replace(/\s/g, "") != '' ? +precision :
3;
switch(precision) {
case 1:
return aDir[Math.floor((deg + 45.0) / 90.0) * 4]; //N,E,S,W,N
case 2:
return aDir[Math.floor((deg + 22.5) / 45.0) * 2]; //N,NE,E,SE,S,SW,W,NW,N
default:
return aDir[Math.floor((deg + 11.25) / 22.5)]; //N,NNE,NE,ENE,E,ESE,SE,SSE,S,SSW,SW,WSW,W,WNW,NW,NNW,N
}
}
//\ZGeoCompassDir ------------------------------------
//=====================================