Commit a7704c1f by lujunyi

根据经纬度排序

parent 81abb609
......@@ -24,6 +24,9 @@ public function test()
public function PharmacyList(Request $request)
{
$search_input = $request->input('search_input');
$lat = $request->input('lat');
$lng = $request->input('lng');
$distance = $request->input('distance_sort', 0); // 经纬度排序规则[0=从近到远,1=从远到近]
$query = PharmacyModel::query();
if ($search_input) {
$query->where('name', 'like', "%{$search_input}%");
......@@ -35,6 +38,15 @@ public function PharmacyList(Request $request)
->where('business_start', '<=', Carbon::now()->format('H:i'))
->where('business_end', '>=', Carbon::now()->format('H:i'));
if ($lat && $lng && is_numeric($lat) && is_numeric($lng)) {
$distancesql = " * , 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";
if ($distance_sort == 1) {
$query = $query->addSelect(DB::raw($distancesql.' as distance'))->orderBy('distance', 'DESC');
} else {
$query = $query->addSelect(DB::raw($distancesql.' as distance'))->orderBy('distance', 'ASC');
}
}
$data = $query->paginate(10);
return $this->success($data);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment