public function getDistance($lat, $lng)
{
$distance = "ACOS(SIN(( $lat * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS(( $lat* 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS(( $lng* 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380";
$list = $this->model->select('*')->addSelect(\DB::raw($distance . ' as distance'))->orderBy('distance', 'ASC')->get()->toArray();
dd($list);
}
distance是計算出來的距離單位是公里
Dome2
$model = DB::table('table_name')->select('');
$model->addSelect(\DB::raw("acos(cos(" . $lat . "*pi()/180)*cos(lat*pi()/180)*cos(" . $lng . "*pi()/180-lng*pi()/180)+sin(" . $lat . "*pi()/180)*sin(lat * pi()/180)) * 6367000 AS distance"))->orderBy('distance','ASC')->get();
本作品採用《CC 協議》,轉載必須註明作者和本文連結