feat: 优化 IndexController 中的距离计算逻辑,提高代码可读性和性能

This commit is contained in:
mkm 2024-08-27 21:10:13 +08:00
parent 79edaa03f1
commit 7967f0b9cd

View File

@ -135,9 +135,8 @@ class IndexController extends BaseApiController
$latitude = $params['lat'];
$longitude = $params['long'];
// 计算距离的SQL表达式
$distanceSql = "SQRT(POW(69.1 * (latitude - {$latitude}), 2) +
POW(69.1 * ({$longitude} - longitude) * COS(latitude / 57.3), 2))";
$find = SystemStore::field("id, name,abbreviation, {$distanceSql} AS distance")
$distanceExpr = "6371 * 2 * ASIN(SQRT(POWER(SIN(({$latitude} - abs(latitude)) * pi()/180 / 2), 2) + COS({$latitude} * pi()/180) * COS(abs(latitude) * pi()/180) * POWER(SIN(({$longitude} - longitude) * pi()/180 / 2), 2)))";
$find = SystemStore::field("id, name,abbreviation, {$distanceExpr} AS distance")
->where('is_show', '=', 1)
->where('latitude', '<>', '')
->where('longitude', '<>', '')