[Help] distance between 2 coordinates

478 views
Skip to first unread message

Handy Trisakti

unread,
Mar 25, 2014, 7:10:33 AM3/25/14
to tas...@googlegroups.com
hi.. 
I tried to create a task to calculate distance between 2 gps coordinates. This task needs to run without any data connection.

so, based on the web below, i created a javascriptlet
http://www.movable-type.co.uk/scripts/latlong.html  (i used phytagoras, because it is simpler & it's only within a city, so it shouldn't differ much)

problem:
it didnt give me error, but did not return anything. If I removed toRad(), at least it gave me numbers (but wrong)

//i just hardcoded these values for simplicity
var lat0=-6.2349792;
var lng0=107.0598804 ;
var lat1=-6.2666051;
var lng1=106.8709742 ;

flash(lat0);

var x = (lng1.toRad()-lng0.toRad()) * Math.cos((lat0.toRad()+lat1.toRad())/2);
var y = (lat1.toRad()-lat0.toRad());
var d = Math.sqrt(x*x + y*y) * R;

setGlobal('TEST', d);


Matt R

unread,
Mar 25, 2014, 2:36:07 PM3/25/14
to tas...@googlegroups.com
Try doing it just in a Variable Set action with "do math" checked.

Matt
Message has been deleted

bakamu

unread,
Mar 25, 2014, 6:47:50 PM3/25/14
to tas...@googlegroups.com
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

Handy Trisakti

unread,
Mar 25, 2014, 9:17:31 PM3/25/14
to tas...@googlegroups.com
wow.. Thank you.. this is a huge help.. 

Delords Arthur

unread,
Apr 20, 2018, 4:51:10 AM4/20/18
to Tasker
Hi, how about a Java Scriptlet that add x metres to a given GPS coordinate?

Dennis Whaley

unread,
May 1, 2019, 1:56:05 PM5/1/19
to Tasker
How would i use this with var? like i have 2 txt edit boxes, one for each full coord, and im using var set %coord1 to %new_val for each box. Now i used this scriptlet but i cant get it to  recognize as a function and i maybe just missing something. I put this in function under java func and it gives me an error not valid function. Please let me know if you see something wrong w the script or if im just messing up all around. I have no idea what im doing in java and have been on code academy and watching youtube vids but no luck figuring this out. 


//============================================

//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 coord1 = pointa.replace(/\s/g, "").split(',');
  var coord2 = pointb.replace(/\s/g, "").split(',');
   
  //simple check of parameters
  if (coord1.length != 2 || coord2.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(coord1[0] * d2r) * Math.sin(coord2[0] * d2r) + Math.cos(coord1[0] * d2r) * Math.cos(coord2[0] * d2r) * Math.cos((coord2[1] - coord1[1]) * d2r));
Reply all
Reply to author
Forward
0 new messages