Example #1
0
 public static function findInLocation($lat, $lng, $max_distance = 25, $units = 'miles', $paginate = true)
 {
     switch ($units) {
         case 'miles':
             //radius of the great circle in miles
             $gr_circle_radius = 3959;
             break;
         case 'kilometers':
             //radius of the great circle in kilometers
             $gr_circle_radius = 6371;
             break;
     }
     $haversine = '(' . $gr_circle_radius . ' * acos(cos(radians(' . $lat . ')) * cos(radians(lat)) * cos(radians(lng) - radians(' . $lng . ')) + sin(radians(' . $lat . ')) * sin(radians(lat))))';
     $radius = 1;
     $locations = DB::table('locations')->select(array('*', DB::raw($haversine . ' as distance')))->orderBy('distance', 'ASC')->having('distance', '<=', $max_distance)->having('locationable_type', '=', 'Spot')->get();
     $spots = [];
     foreach ($locations as $loc) {
         $location = Location::find($loc->id);
         if ($location->locationable && $location->locationable->status == 'Publish') {
             array_push($spots, $location->locationable);
         }
     }
     $collection = new Illuminate\Support\Collection($spots);
     $collection->sort(function ($a, $b) {
         return $a->created_at->lt($b->created_at);
     });
     if ($paginate) {
         $page = 1;
         if (Input::has('page')) {
             $page = Input::get('page');
         }
         $perPage = 200;
         $offset = ($page - 1) * $perPage;
         return Paginator::make($collection->slice($offset, $perPage, true)->all(), $collection->count(), $perPage);
     }
     return $collection;
 }