/**
* 根据门店距离排序
* @param $data
* @param string $longitude 经度
* @param string $latitude 纬度
* @return array
*/
public function sortByDistance($longitude,$latitude)
{
// 根据距离排序
$list =Db::table('benben_store_shop')
->where('status',1)
->where('is_delete',0)
->order('id desc')
->select();
$sortArr = [];
foreach ($list as &$shop) {
// 计算距离
$distance = self::getDistance((float)$longitude, (float)$latitude, (float)$shop['longitude'], (float)$shop['latitude']);
// 排序列
$sortArr[] = $distance;
$shop['distance'] = $distance;
if ($distance >= 1000) {
$distance = bcdiv((string)$distance, (string)1000, 2);
$shop['distance_unit'] = $distance . 'km';
} else
$shop['distance_unit'] = $distance . 'm';
}
// 根据距离排序
array_multisort($sortArr, SORT_ASC, $list);
return json($list);;
}
/**
* 获取两个坐标点的距离
* @param float $ulon
* @param float $ulat
* @param float $slon
* @param float $slat
* @return float
*/
private static function getDistance(float $ulon, float $ulat, float $slon, float $slat)
{
// 地球半径
$R = 6378137;
// 将角度转为弧度
$radLat1 = deg2rad($ulat);
$radLat2 = deg2rad($slat);
$radLng1 = deg2rad($ulon);
$radLng2 = deg2rad($slon);
// 结果
$s = acos(cos($radLat1) * cos($radLat2) * cos($radLng1 - $radLng2) + sin($radLat1) * sin($radLat2)) * $R;
// 精度
$s = round($s * 10000) / 10000;
return round($s);
}
标签:shop,直线,经纬度,distance,float,距离,deg2rad,param,latitude
From: https://www.cnblogs.com/lakix/p/18136706