ShowTable of Contents
calculate the distance between two points
Found at Clearly Innovative Thoughts
Number.prototype.toDeg = function() {
return this * 180 / Math.PI;
}
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
var lat1,lon1,lat2,lon2;
// example latitude and longitude position
lat1=40.28;
lon1=-74.82;
lat2=40.17;
lon2=-47.12
var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
// here is the distance in km
var d = R * c;
Ti.API.debug("km " + d);
Ti.API.debug("miles " + d/1.609344);