Skip to content

Instantly share code, notes, and snippets.

@broox
Created April 15, 2012 15:56
Show Gist options
  • Save broox/2393548 to your computer and use it in GitHub Desktop.
Save broox/2393548 to your computer and use it in GitHub Desktop.
Haversine Distance Formulas

Haversine Formula

used to calculate the great circle distance between two points on the earth (specified in decimal degrees)

Earth's Radius

  • Miles: 3956
  • Kilometers: 6371

Javascript

// Convert numeric degrees to radians
if (typeof(Number.prototype.toRad) === 'undefined') {
  Number.prototype.toRad = function() {
    return this * Math.PI / 180;
  }
}

function distance(lat1, lng1, lat2, lng2) {
  var dLat = (lat2-lat1).toRad(),
      dLng = (lng2-lng1).toRad(),
      lat1 = lat1.toRad(),
      lat2 = lat2.toRad();

  var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
          Math.sin(dLng / 2) * Math.sin(dLng / 2) * 
          Math.cos(lat1) * Math.cos(lat2),
      c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

  return 3956 * c;
}

Coffeescript

toRad = (val) ->
  val * Math.PI / 180

distance = (lat1, lng1, lat2, lng2) ->
  dLat = toRad(lat2-lat1)
  dLng = toRad(lng2-lng1)
  a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLng/2) * Math.sin(dLng/2)
  c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
  3956 * c

PHP

function distance($lat1, $lng1, $lat2, $lng2) {

    // convert decimal degrees to radians
    $dLat = deg2rad($lat2 - $lat1);
    $dLng = deg2rad($lng2 - $lng1);
    $lat1 = deg2rad($lat1);
    $lat2 = deg2rad($lat2);

    // haversine formula
    $a = sin($dLat / 2) * sin($dLat / 2) +
         sin($dLng / 2) * sin($dLng / 2) *
         cos($lat1) * cos($lat2);
    $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
    return 3956 * $c;
}

Python

def distance(self, lat1, lng1, lat2, lng2):
    """
    Calculate the great circle distance (in miles) between two points
    on the earth (specified in decimal degrees)
    """

    # convert decimal degrees to radians
    lng1, lat1, lng2, lat2 = map(radians, [lng1, lat1, lng2, lat2])
    dlng = lng2 - lng1
    dlat = lat2 - lat1

    # haversine formula
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlng/2)**2
    c = 2 * asin(sqrt(a))
    return 3956 * c

Ruby

class Geo
  EARTH_RADIUS = 3956
  RADIANS_PER_DEGREE = 0.017453293

  # Uses the haversine formula to calculate the great-circle distance between two points (in miles)
  def self.distance( lat1, lng1, lat2, lng2 )
    dlng = lng2 - lng1
    dlat = lat2 - lat1

    dlng_rad = dlng * RADIANS_PER_DEGREE
    dlat_rad = dlat * RADIANS_PER_DEGREE

    lat1_rad = lat1 * RADIANS_PER_DEGREE
    lat2_rad = lat2 * RADIANS_PER_DEGREE

    a = (Math.sin(dlat_rad/2))**2 + Math.cos(lat1_rad) * Math.cos(lat2_rad) * (Math.sin(dlng_rad/2))**2
    c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a))

    EARTH_RADIUS * c
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment