– Common Function Library Project

LatLonDist(lat1, lon1, lat2, lon2, units)

Last updated May 14, 2002

Version: 1 | Requires: ColdFusion 5 | Library: MathLib

Rated 5 time(s). Average Rating: 4.6

Calculates the distance between two latitudes and longitudes. Results can be in radians, kilometers, statute miles or nautical miles. Handles the special cases of starting at either pole and checks for valid values.

Return Values:
Returns a number or an error string.


view plain print about
Suppose point 1 is LAX: (33deg 57min N, 118deg 24min W)<br>
Suppose point 2 is JFK: (40deg 38min N,  73deg 47min W)

  Lat1 = 33.95;
  Lon1 = 118.4;
  Lat2 = 40.63333;
  Lon2 = 73.7833;

Lat1 = #lat1#<br>
Lon1 = #lon1#<br>
Lat2 = #lat2#<br>
Lon2 = #lon2#<br>

Distance = #LatLonDist(lat1,lon1,lat2,lon2,'nm')# nautical miles<br>
Distance = #LatLonDist(lat1,lon1,lat2,lon2,'sm')# statute miles<br>
Distance = #LatLonDist(lat1,lon1,lat2,lon2,'km')# kilometers<br>
Distance = #LatLonDist(lat1,lon1,lat2,lon2,'radians')# radians<br>


Name Description Required
lat1 Latitude of the first point in degrees. Yes
lon1 Longitude of the first point in degrees. Yes
lat2 Latitude of the second point in degrees. Yes
lon2 Longitude of the second point in degrees. Yes
units Unit to return distance in. Options are: km (kilometers), sm (statute miles), nm (nautical miles), or radians. Yes

Full UDF Source:

view plain print about
 * Calculates the distance between two latitudes and longitudes.
 * This funciton uses forumlae from Ed Williams Aviation Foundry website at
 * @param lat1      Latitude of the first point in degrees. (Required)
 * @param lon1      Longitude of the first point in degrees. (Required)
 * @param lat2      Latitude of the second point in degrees. (Required)
 * @param lon2      Longitude of the second point in degrees. (Required)
 * @param units      Unit to return distance in. Options are: km (kilometers), sm (statute miles), nm (nautical miles), or radians.  (Required)
 * @return Returns a number or an error string. 
 * @author Tom Nunamaker ( 
 * @version 1, May 14, 2002 

function LatLonDist(lat1,lon1,lat2,lon2,units)
  // Check to make sure latitutdes and longitudes are valid
  if(lat1 GT 90 OR lat1 LT -90 OR
     lon1 GT 180 OR lon1 LT -180 OR
     lat2 GT 90 OR lat2 LT -90 OR
     lon2 GT 280 OR lon2 LT -280) {
    Return ("Incorrect parameters");

  lat1 = lat1 * pi()/180;
  lon1 = lon1 * pi()/180;
  lat2 = lat2 * pi()/180;
  lon2 = lon2 * pi()/180;
  UnitConverter = 1.150779448;  //standard is statute miles
  if(units eq 'nm') {
    UnitConverter = 1.0;
  if(units eq 'km') {
    UnitConverter = 1.852;
  distance = 2*asin(sqr((sin((lat1-lat2)/2))^2 + cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2));  //radians
  if(units neq 'radians'){
    distance = UnitConverter * 60 * distance * 180/pi();
  Return (distance) ;
blog comments powered by Disqus


Latest Additions

Troy Pullis Troy Pullis added
18 day(s) ago

Henry Ho Henry Ho added
a while ago

Henry Ho Henry Ho added
a while ago

Tony Felice Tony Felice added
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 44 time(s)

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 8 time(s)

Markus Schneebeli                                 ListRemoveByStri...
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson