1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
-
- <?php
- header('Content-Type: application/json');
- function distanceMeters($lat1, $lon1, $lat2, $lon2)
- {
- $x = deg2rad( $lon1 - $lon2 ) * cos( deg2rad( $lat1 ) );
- $y = deg2rad( $lat1 - $lat2 );
- $dist = 6371000.0 * sqrt( $x*$x + $y*$y );
-
- return $dist;
- }
- $db = file('db.txt');
- $sum_of_lat = 0;
- $sum_of_long = 0;
- $sum_of_time = 0;
- $array_time = array();
- $average = new stdClass;
- $average->latitude = 0;
- $average->longitude = 0;
- $average->inTime = 0;
- $average->parada = '';
- $db_size = count($db);
- $distances=[];
- $paradas = array(
- "Generales" => array(18.404916, -66.044828),
- "Natu_1" => array(18.403663, -66.045529),
- "Natu_2" => array(18.403769, -66.046572),
- "Centro" => array( 18.403898, -66.048255),
- "Biblio" => array(18.403994, -66.049781),
- "Merenderos" => array(18.406345, -66.050197),
- "Leyes" => array (18.406069, -66.047733),
- "Complejo" => array(18.406503, -66.046121),
- "ROTC_Entrada" => array(18.406083, -66.043605),
- "ROTC_Estacionamiento" => array( 18.407121, -66.042053)
- );
- for ($i = 0; $i < $db_size; $i++)
- {
- $decoded_location = json_decode($db[$i]);
- $sum_of_lat += $decoded_location->latitude;
- $sum_of_long += $decoded_location->longitude;
- array_push($array_time,$decoded_location->inTime);
- if($i+1 == $db_size)
- {
- $average->latitude = $sum_of_lat/$db_size;
- $average->longitude = $sum_of_long/$db_size;
- $average->inTime = date("h:i:s a",(array_sum(array_map('strtotime',$array_time)))/$db_size);
- }
- }
-
- foreach($paradas as &$thing)
- {
- $plo = distanceMeters($average->latitude, $average->longitude, $thing[0], $thing[1]);
- array_push($distances,$plo);
- //echo $distances;
-
- }
-
- //echo $encoded_average;
- $indice = array_search(min($distances),$distances);
- $checking = array_keys($paradas);
- //$encoded_average = json_decode($encoded_avarage);
-
- $average->parada = $checking[$indice];
- $encoded_average =json_encode($average);
- echo $encoded_average;
- //echo "pol";
- //echo $average;
- ?>
|