1. /**
    2. * 根据经纬度计算两点间距离
    3. * @param int $lat1
    4. * @param int $lon1
    5. * @param int $lat2
    6. * @param int $lon2
    7. * @param string $unit
    8. * @return float
    9. */
    10. public static function distance($lat1, $lon1, $lat2, $lon2, $unit = "K")
    11. {
    12. $theta = $lon1 - $lon2;
    13. $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    14. $dist = acos($dist);
    15. $dist = rad2deg($dist);
    16. $miles = $dist * 60 * 1.1515; //此处返回值单位为英里
    17. $unit = strtoupper($unit);
    18. //单位转换
    19. if ($unit == "K") {
    20. return ($miles * 1.609344);//1英里=1.609344公里
    21. } else if ($unit == "N") {
    22. return ($miles * 0.8684);//1英里=0.8684海里
    23. } else { //mi
    24. return $miles;//英里
    25. }
    26. }