Skip to main content link. Accesskey S

The useful resource for Titanium and YouAtNotes Domino To Go

Submit Search

Home > Titanium > Ti: work with maps

Ti: work with maps

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);
Created by bob on Dec 5, 2011 11:08:53 AM

Math.atan2(Math.sqrt(a), Math.sqrt(1-a))

should be written as

Math.asin(Math.sqrt(a))


Add Comment

Name:
Comments:
Use  searchlotus.com  for news in the Web related to Lotus Notes and Domino,
and to search those sites.
Check  youatnotes.com  for great Lotus Notes, Domino and XPages software.