Hi Handy Trisakti,
here a tested javscriptlet distance function (ZDistance):
//============================================
//Test the function ZDistance
var retvar = ZDistance('-6.2349792, 107.0598804', '-6.2666051, 106.8709742');
alert('Distance: ' + retvar + ' m');
//----------------------------
//------------------------------------
//ZDistance (pointa, pointb, dp, unit)
//Params: {string} pointa,pointb: strings of Latitude,Longitude of points (eg '40.748785, -73.984902','40.742747, -73.972632');
// {number, optional} dp: number of decimal places to use in the returned distance (default: 0)
// {number, optional} unit: 0: meter , 1: kilometer , 2: yard, 3: miles, 4: nautical miles (default : meter)
//Return: {number} distance between pointa and pointb in given units
//------------------------------------
function ZDistance (pointa, pointb, dp, unit)
{
var d2r = Math.PI / 180.0; //Javascript functions in radians , (x * Math.PI / 180) = toRad(x)
var earthradius = 6378137; //Radius of the earth in m, 6378.137 in Km, 3963.191 in Miles, 6975215 in Yards
var reterr = 'na';
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(',');
var pb = pointb.replace(/\s/g, "").split(',');
//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;}
//check decimal places
dp = typeof(dp) == 'number' ? dp : typeof(dp) == 'string' && dp.replace(/^\s+|\s+$/g, '') != '' ? +dp : NaN;
if (isNaN(dp) || dp < 0 || dp > 10) {dp = 0;}
//check unit parameter
unit = typeof(unit) == 'number' ? unit : typeof(unit) == 'string'
&& unit.replace(/^\s+|\s+$/g, '') != '' ? +unit : NaN;
if (isNaN(unit) || unit < 0 || unit > 4) {unit = 0;}
//calculate distance between point a and point b
var distance = earthradius * Math.acos(Math.sin(pa[0] * d2r) *
Math.sin(pb[0] * d2r) + Math.cos(pa[0] * d2r) * Math.cos(pb[0] * d2r) *
Math.cos((pb[1] - pa[1]) * d2r));
distance = Math.abs(distance);
switch (unit) {
case 0: // meter m
break;
case 1: // kilometer km
distance = distance * 0.001;
break;
case 2: // yard yd
distance = distance * 1.09361329833771;
break;
case 3: // mile mi
distance = distance * 0.000621371192237334;
break;
case 4: // nautical mile nmi
distance = distance * 0.000539956803455724;
break;
}
return distance.toFixed(dp);
}
//\ZDistance ------------------------------------
//==============================================
bakamu