From cd6f4bc2800952c50bcc434b18ba263581a99b1b Mon Sep 17 00:00:00 2001 From: Lemon <15040771@qq.com> Date: Fri, 4 Sep 2020 17:20:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E4=BD=8D=E8=AF=A6=E6=83=85=E9=A1=B5?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E8=B7=9D=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../v3/Implementations/LocationService.php | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/app/Service/v3/Implementations/LocationService.php b/app/Service/v3/Implementations/LocationService.php index c828cf6..9cfd504 100644 --- a/app/Service/v3/Implementations/LocationService.php +++ b/app/Service/v3/Implementations/LocationService.php @@ -26,6 +26,37 @@ class LocationService implements LocationServiceInterface public function getMarketListByLocation($cityIds) { $res = Area::query()->with('markets')->whereIn('id',$cityIds)->get(); + foreach ($res as &$v){ + foreach ($v->markets as &$m) + { + $m->Distance = $this->getDistance($m->lng,$m->lat,108.370333,22.813527); + } + } return $res; } + + function getDistance($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2) + { + + $EARTH_RADIUS = 6370.996; // 地球半径系数 + $PI = 3.1415926535898; + + $radLat1 = $lat1 * $PI / 180.0; + $radLat2 = $lat2 * $PI / 180.0; + + $radLng1 = $lng1 * $PI / 180.0; + $radLng2 = $lng2 * $PI / 180.0; + + $a = $radLat1 - $radLat2; + $b = $radLng1 - $radLng2; + + $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))); + $distance = $distance * $EARTH_RADIUS * 1000; + + if ($unit === 2) { + $distance /= 1000; + } + + return round($distance, $decimal); + } } \ No newline at end of file