You are here

public function IpGeoLocAPI::earthDistance in IP Geolocation Views & Maps 8

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

File

src/Services/IpGeoLocAPI.php, line 735

Class

IpGeoLocAPI
Class IpGeoAPI to interact with other modules.

Namespace

Drupal\ip_geoloc\Services

Code

public function earthDistance($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;
}