|
|
|
@ -4,6 +4,7 @@ |
|
|
|
namespace App\Service\v3\Implementations; |
|
|
|
use App\Constants\v3\ErrorCode; |
|
|
|
use App\Exception\ErrorCodeException; |
|
|
|
use App\Model\v3\UserCollection; |
|
|
|
use App\Service\v3\Interfaces\LocationServiceInterface; |
|
|
|
use App\Model\v3\Area; |
|
|
|
use App\Model\v3\Market; |
|
|
|
@ -59,6 +60,31 @@ class LocationService implements LocationServiceInterface |
|
|
|
return $res; |
|
|
|
} |
|
|
|
|
|
|
|
public function getNearestMarket($lng,$lat) |
|
|
|
{ |
|
|
|
$markets = Market::query()->get()->toArray(); |
|
|
|
$result = []; |
|
|
|
$distinctMin = 0; |
|
|
|
foreach ($markets as &$market){ |
|
|
|
$distinct = $this->getDistance($lng,$lat,$market['lng'],$market['lat'],1); |
|
|
|
$market['distinct'] = round(($distinct/1000),2).' km'; |
|
|
|
if($distinct < $distinctMin || $distinctMin === 0) { |
|
|
|
$distinctMin = $distinct; |
|
|
|
$result = $market; |
|
|
|
} |
|
|
|
} |
|
|
|
return $result; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $lng1 |
|
|
|
* @param $lat1 |
|
|
|
* @param $lng2 |
|
|
|
* @param $lat2 |
|
|
|
* @param int $unit 单位 1:米 2:公里 |
|
|
|
* @param int $decimal 精度 保留小数位数 |
|
|
|
* @return false|float |
|
|
|
*/ |
|
|
|
public function getDistance($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2) |
|
|
|
{ |
|
|
|
|
|
|
|
|