Built From Developers For Developers
» Kuikie.com » Snippets » PHP » Other » PHP. Calculate Distance Between Two Locations Using Their Geographical Coordinates in Kilometers, Miles or Nautical Miles
  Categories
  PHP
    Arrays
    Date & Time
    Files & Directories
    Globalization
    Images
    OOP
    Other
    Strings
    User Interface
 

PHP. Calculate Distance Between Two Locations Using Their Geographical Coordinates in Kilometers, Miles or Nautical Miles

The following PHP snippet calculates the distance between two locations using their geographical coordinates and returns the distance in kilometers, miles or nautical miles. The functionality is placed in a utility function calculate_distance, which accepts five arguments. The first four arguments are the geographical coordinates (latitude and longitude) of the locations. The fifth argument specifies the unit (kilometer, miles or nautical miles), which we want as output. Because of the different spellings, the function actually takes into account only the first letter of the unit. Thus it will work with almost any unit designation (K,Km,kilometer,kilometers), and even spelling mistakes (kilometr,kilometre,kilometres).

function calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, $unit="M"){
    $unit = substr(strtoupper($unit),0,1);
    $units = array('K','M','N');
    if (in_array($unit, $units) == false) {
    	throw new RuntimeException("Unit ".$unit." is not supported. Only kilometers, miles and nautical miles are supported!");
    }
    
    $dist = sin(deg2rad($latitude1)) * sin(deg2rad($latitude2)) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($longitude1 - $longitude2));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles  = (float) $dist * 60 * 1.1515;
    
    if ($unit == "K") { return ($miles * 1.609344); }
    if ($unit == "N") { return ($miles * 0.8684); }
    return $miles;
}

Example

$latitude1 = 46.2341938036161; 
$longitude1 = -63.124778021257015; 

$latitude2 = 46.4141938036161; 
$longitude2 = -63.118571124705284;

// Get distance in kilometers
echo calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, 'K');
echo calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, 'KM');
echo calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, 'k');
echo calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, 'km');
echo calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, 'kilometer');
echo calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, 'kilometers');
echo calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, 'kilometre');
echo calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, 'kilometres');

// Get distance in miles
echo calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, 'M');
echo calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, 'm');
echo calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, 'mile');
echo calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, 'miles');

// Get distance in nautical miles
echo calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, 'N');
echo calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, 'n');
echo calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, 'NM');
echo calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, 'nm');
echo calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, 'nmiles');
echo calculate_distance($latitude1, $longitude1, $latitude2, $longitude2, 'nautical miles');
 
©2012 Sinevia Ltd Terms of Use         Privacy Policy Valid XHTML 1.0 Transitional