Tuesday, January 8, 2008

Latitude and Longitude Distance Calculations

A few days ago for one of my projects, I got the wonderful task of determining a distance in terms of latitudinal/longitudinal degrees. This actually turns out to be a quite simple task, however it took some searching and some calculations for me to actually figure this out. So, to save me (and possibly someone else) some time in the future, I'll list some basic latitudinal/longitudinal distance facts here.

  • The distance between two adjacent degrees of latitude is 69.172 miles.
    • So, the distance between two points of latitude is:
      • x = abs(lat1 - lat2) / 69.172
  • The distance between to adjacent degrees of longitude is: cos(latitude) * 69.172
    • So, the distance between two points of longitude is:
      • y = abs(lng1 - lng2) / (cos(latitude) & 69.172)
  • The distance between the two global coordinates (lat1, lng1) and (lat2, lng2) is:
    • Separated method:
      • x = abs(lat1 - lat2) / 69.172
      • y = abs(lng1 - lng2) / (cos((lat1 + lat2) / 2) * 69.172)
      • dist = sqrt(x * x + y * y)
    • One line:
      • dist = sqrt(pow(abs(lat1 - lat2) / 69.172, 2) + pow( abs(lng1 - lng2) / (cos((lat1 + lat2) / 2) * 69.172), 2))
  • A point, p, that is x miles from point (lat1, lng1) (or a point on a circle with a radius of x miles and a center of (lat1, lng1) can be calculated as follows:
    • p(latitude, longitude) = (lat1 + x / 69.172, lng1)