public function get_distance_list(){
$param = $this->request->param();
$EARTH = 6378.137; //地球半径
$PI = 3.1415926535898; //PI值
$lng = $param['lng']; //经度
$lat = $param['lat']; //纬度
$res = Db::name('maps')->alias('mp')
->field("sp.*,mp.*,(2 * $EARTH* ASIN(SQRT(POW(SIN($PI*(" . $lat . "-lat)/360),2)+COS($PI*" . $lat . "/180)* COS(lat * $PI/180)*POW(SIN($PI*(" . $lng . "-lng)/360),2)))) as distance")
->join('shop sp','sp.id = mp.shop_id')
->order('distance asc')
->select();
if ($res) {
foreach ($res as $k => $v) {
$res[$k]['distance'] = $this->get_distance($lng, $lat, $v['lng'], $v['lat']);
// $res[$k]['image'] = $this->request->domain() . $v['image'];
}
}
$this->success('数据成功', $res);
}
##获取单个
public function get_distance($lng1, $lat1, $lng2,$lat2) {
// 将角度转为狐度
$radLat1 = deg2rad($lat1); //deg2rad()函数将角度转换为弧度
$radLat2 = deg2rad($lat2);
$radLng1 = deg2rad($lng1);
$radLng2 = deg2rad($lng2);
$a = $radLat1 - $radLat2;
$b = $radLng1 - $radLng2;
$s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
return number_format($s/1000,1);
}
|