You are here

function ip_geoloc_earth_distance in IP Geolocation Views & Maps 7

Same name and namespace in other branches
  1. 8 ip_geoloc_api.inc \ip_geoloc_earth_distance()

Returns the distance between two points on the earth's surface.

The points are defined by their lat/long coordinates.

Gratefully copied from the http://drupal.org/project/location module, thus ensuring compatibility of results.

Parameters

float $longitude1: Must be in range -180..180.

float $latitude1: Must be in range -90..90.

float $longitude2: Must be in range -180..180.

float $latitude2: Must be in range -90..90

Return value

float Distance between the two points in meters.

See also

http://en.wikipedia.org/wiki/Great-circle_distance

1 call to ip_geoloc_earth_distance()
ip_geoloc_distance in ./ip_geoloc_api.inc
Returns the distance (in meters) between two points on the earth's surface.

File

./ip_geoloc_api.inc, line 722
API functions of IP geolocation module

Code

function ip_geoloc_earth_distance($longitude1, $latitude1, $longitude2, $latitude2) {
  $long1 = deg2rad($longitude1);
  $lat1 = deg2rad($latitude1);
  $long2 = deg2rad($longitude2);
  $lat2 = deg2rad($latitude2);

  // $long_factor = cos($long1) * cos($long2) + sin($long1) * sin($long2);
  // This is identical to this $long_factor = cos($long1 - $long2).
  $long_factor = cos($long1 - $long2);
  $cosangle = cos($lat1) * cos($lat2) * $long_factor + sin($lat1) * sin($lat2);
  $radius = ip_geoloc_earth_radius(0.5 * ($latitude1 + $latitude2));
  $distance = acos($cosangle) * $radius;

  /*
  if ($distance < 1000) {
  // see http://en.wikipedia.org/wiki/Haversine_formula
  $sinlat = sin(0.5*($lat1 - $lat2));
  $sinlong = sin(0.5*($long1 - $long2));
  $sinangle = $sinlat*$sinlat + cos($long1)*cos($long2)*$sinlong*$sinlong;
  $distance = 2.0 * asin(sqrt($sinangle)) * $radius;
  }
  */
  return $distance;
}