CodeIgniter Forums
Calculate all locations distance - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Calculate all locations distance (/thread-74690.html)



Calculate all locations distance - omid_student - 10-25-2019

Hi
I have project named GPS Tracker
The one hundred device is active in my project
Each device send 24000 location to my server
In continue,i need calculate distance this locations for each device and save it
But number of records is high and if i use FOR statement for iterate array and calculate distance,the CPU maybe down
I try calculate distance between location when device send to server but locations is irregular and i cannot calculate exact distance
So i have to calculate distance after receive locations and insert into database and sort it and calculate distance

My code is :
PHP Code:
$this->db->order_by('time','ASC');
        
$this->db->where('device_id',$device_id);
        
$this->db->where('date',$date);
        
$locations $this->db->get('location')->result_array();

        
$last_location  =   '';
        
$distance       =   0;

        foreach (
$locations as $location) {

            if (
$last_location != '') {

                
$temp distance($last_location['latitude'],$last_location['longitude'],$location['latitude'],$location['longitude'],'m');

                if (!
is_nan($temp))
                    
$distance += $temp;

            }

            
$last_location $location;

        } 

Please suggest solution for me
Thanks