public function calculateDistance($lat1, $lon1, $lat2, $lon2) { // 将角度转换为弧度 $lat1Rad = deg2rad($lat1); $lon1Rad = deg2rad($lon1); $lat2Rad = deg2rad($lat2); $lon2Rad = deg2rad($lon2); // Haversine公式 $earthRadius = 6371; // 地球半径,单位为公里 $deltaLat = $lat2Rad - $lat1Rad; $deltaLon = $lon2Rad - $lon1Rad; $a = pow(sin($deltaLat / 2), 2) + cos($lat1Rad) * cos($lat2Rad) * pow(sin($deltaLon / 2), 2); $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); $distance = $earthRadius * $c; return $distance; }
点击 "运行实例" 按钮查看在线实例
$distance = $this->calculateDistance($lat1, $lon1, $lat2, $lon2); $return_distance = "距离您".round($distance,1)."km"; if($distance<1){ $return_distance = "距离您".floor($distance*1000)."m"; }
点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号