$lon1 經度
$lat1 緯度
$lon2 經度
$lat2 緯度
$pi80 = 3.1415926535898 / 180;
$lat1 *= $pi80;
$lon1 *= $pi80;
$lat2 *= $pi80;
$lon2 *= $pi80;
//$r = 6372.797; // mean radius of Earth in km
$r = 6378.137;
$dlat = $lat2 - $lat1;
$dlng = $lon2 - $lon1;
$a = sin($dlat/2)*sin($dlat/2)+cos($lat1)*cos($lat2)*sin($dlng/2)*sin($dlng/2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
$km = $r * $c;