Nearby stores in Laravel

前端 未结 1 1228
挽巷
挽巷 2021-01-14 11:20

i\'m try to get neraby store in Laravel 5.1 I have geocoding parser that caluclate coorinate. But i have problem with haversine formulas. Basically i need that from table Az

相关标签:
1条回答
  • 2021-01-14 11:58

    The following should work for you to implement the haversine formula in an SQL query. My answer is assuming you have a model setup to correspond with the database table "aziende".

    $lat = floatval($response['results'][0]['geometry']['location']['lat']);
    $lng = floatval($response['results'][0]['geometry']['location']['lng']);
    $radius = 5;
    $distance = DB::raw("*, ( 6371 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance");
    $aziende = Aziende::select($distance)->orderBy('distance')->where('distance', '<=', $radius)->get();
    
    0 讨论(0)
提交回复
热议问题