[PHP]計算二點間距離

#lat1,lon1是第一個點的經緯度
#lat2,lon2是第二個點的經緯度
#unit的話就用"K" (這樣的話傳回值就會是公尺)
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
  global $source;
  $theta = $lon1 - $lon2;
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  $dist = acos($dist);
  $dist = rad2deg($dist);
  $miles = $dist * 60 * 1.1515;
  $unit = strtoupper($unit);

  if ($unit == "K") {
    $meters = intval($miles * 1.609344 * 1000);
    if($meters > 10000){
      return intval($meters/1000).msg('公里','km');
    }else{
      return $meters.msg('公尺','m');
    }
  } else if ($unit == "N") {
    return ($miles * 0.8684);
  } else {
    return $miles;
  }
}