dimanche 28 juin 2015

Ajax calls in an interval slows down map

I am building a tracking app that takes locations from a database that is updated frequently and put the last updated data as the marker..I use a ajax call in an interval that is called in every second to retrieve live updating data..

 $.ajax({
     type: "POST",
     async:false,
     dataType: "json",
     url: "{{ url('/tracking_mysql/bt6pmSWHTfnOAjfJMlSvRQ==') }}",
     success: function(data)
     {
      @foreach($eventUsers as $eventUser)
        markers['marker' + '{{$eventUser->userId}}'] = null ;
      @endforeach
      $.each(data,function(u,v){
      markers['marker' + v['user_id']] = new google.maps.LatLng(v['Location'].split(' ')[0],v['Location'].split(' ')[1]);
     lastUpdatedTimes['lastUpdatedTime' + v['user_id']] = v['time'];
   });
  }
});

and the controller that passes the current locations is :

 public function get_locations($eventid)
    {
            $eventId = $event_group_Idarray[0];
            $locations = DB::table('locations')
                ->select('time', 'Location','user_id')
                ->join(DB::raw('( SELECT MAX(id) as Id FROM locations GROUP BY user_id ) AS newest'), function($join)
                {
                    $join->on('locations.id', '=', 'newest.id');
                })
                ->where('event_id',$eventId)
                ->get();

            print_r(json_encode($locations));
    }

the problem is during each ajax call the map updation freezes for a moment.I am sure the problem is using ajax calls in an interval..I commented the ajax call and passed random markers in an interval,the map was smooth..is there any solution that can be used for my purpose ?

Aucun commentaire:

Enregistrer un commentaire