From a53972ecc17b13cc5ca8359022833e48f178e8c9 Mon Sep 17 00:00:00 2001 From: cody <648753004@qq.com> Date: Thu, 28 Aug 2025 14:50:41 +0800 Subject: [PATCH] update --- app/Http/Controllers/Mobile/OtherController.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Mobile/OtherController.php b/app/Http/Controllers/Mobile/OtherController.php index ea1ac29..8588c41 100755 --- a/app/Http/Controllers/Mobile/OtherController.php +++ b/app/Http/Controllers/Mobile/OtherController.php @@ -140,23 +140,24 @@ class OtherController extends CommonController $sortName = $all['sort_name'] ?? 'distance'; $sortType = $all['sort_type'] ?? 'asc'; - // 使用 Haversine 公式计算距离,确保经纬度数据有效 + // 使用更稳定的距离计算公式,避免 acos 的精度问题 $distanceFormula = " - (6371 * acos( - cos(radians(?)) * - cos(radians(CAST(company_latitude AS DECIMAL(10,8)))) * - cos(radians(CAST(company_longitude AS DECIMAL(10,8))) - radians(?)) + - sin(radians(?)) * - sin(radians(CAST(company_latitude AS DECIMAL(10,8)))) + (6371 * 2 * asin( + sqrt( + pow(sin(radians((CAST(company_latitude AS DECIMAL(10,8)) - ?) / 2)), 2) + + cos(radians(?)) * cos(radians(CAST(company_latitude AS DECIMAL(10,8)))) * + pow(sin(radians((CAST(company_longitude AS DECIMAL(10,8)) - ?) / 2)), 2) + ) )) AS distance "; $query = Company::with('users')->select('*') + ->selectRaw($distanceFormula, [$latitude, $latitude, $longitude]) ->where(function ($query) use ($all) { if (isset($all['company_name'])) { $query->where('company_name', 'like', '%' . $all['company_name'] . '%'); } - })->selectRaw($distanceFormula, [$latitude, $longitude, $latitude]) + }) ->whereNotNull('company_longitude') ->whereNotNull('company_latitude') ->where('company_longitude', '!=', '')