You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.1 KiB
112 lines
3.1 KiB
<?php
|
|
|
|
|
|
namespace App\Service\v3\Implementations;
|
|
use App\Constants\v3\ErrorCode;
|
|
use App\Exception\ErrorCodeException;
|
|
use App\Service\v3\Interfaces\LocationServiceInterface;
|
|
use App\Model\v3\Area;
|
|
use App\Model\v3\Market;
|
|
class LocationService implements LocationServiceInterface
|
|
{
|
|
|
|
public function do()
|
|
{
|
|
// TODO: Implement do() method.
|
|
}
|
|
|
|
public function check()
|
|
{
|
|
// TODO: Implement check() method.
|
|
}
|
|
|
|
public function undo()
|
|
{
|
|
// TODO: Implement undo() method.
|
|
}
|
|
|
|
public function getMarketListByLocation($lng,$lat)
|
|
{
|
|
$cityIds = Market::query()->pluck('city_id');
|
|
$res = Area::query()->with('markets')->whereIn('id',$cityIds)->get();
|
|
foreach ($res as &$v){
|
|
foreach ($v->markets as &$m)
|
|
{
|
|
if(!empty($lng) && !empty($lat)){
|
|
$m->Distance = ($this->getDistance($m->lng,$m->lat,$lng,$lat)).' km';
|
|
}else{
|
|
$m->Distance = '';
|
|
}
|
|
}
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
public function searchMarket($keywords,$lng,$lat,$cityId = 2163)
|
|
{
|
|
$res = Market::query()->where([
|
|
['name','like','%'.$keywords.'%'],
|
|
['city_id','=',$cityId]
|
|
])
|
|
->get();
|
|
foreach ($res as &$v){
|
|
if(!empty($lng) && !empty($lat)){
|
|
$v->Distance = ($this->getDistance($v->lng,$v->lat,$lng,$lat)).' km';
|
|
}else{
|
|
$v->Distance = '';
|
|
}
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
public 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);
|
|
}
|
|
|
|
//获取服务站信息
|
|
function getMarketsInfo()
|
|
{
|
|
return Market::where('status',1)->get();
|
|
}
|
|
|
|
//获取骑行距离
|
|
public function getDistanceByTencent($lng1, $lat1, $lng2, $lat2)
|
|
{
|
|
$url = 'https://apis.map.qq.com/ws/direction/v1/';
|
|
/**
|
|
* $tyep driving 驾车 walking 步行 bicycling 骑行 transit 公交
|
|
*/
|
|
$type = 'bicycling';
|
|
$key = 'GB3BZ-7W2CU-LW3VH-B7ZIF-XRKSF-D3FOL';
|
|
$result = file_get_contents($url.$type.'/?from='.$lat1.','.$lng1.'&to='.$lat2.','.$lng2.'&output=json&key='.$key);
|
|
$resultArr = json_decode($result,true);
|
|
if($resultArr['status'] > 0)
|
|
{
|
|
return 1000;
|
|
//throw new ErrorCodeException(ErrorCode::LOCATION_ERROR,$resultArr['message']);
|
|
}
|
|
$distance = $resultArr['result']['routes'][0]['distance'];
|
|
return $distance;
|
|
}
|
|
}
|