Repositorio del curso CCOM4030 el semestre B91 del proyecto Trolley

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. header('Content-Type: application/json');
  3. function distanceMeters($lat1, $lon1, $lat2, $lon2)
  4. {
  5. $x = deg2rad( $lon1 - $lon2 ) * cos( deg2rad( $lat1 ) );
  6. $y = deg2rad( $lat1 - $lat2 );
  7. $dist = 6371000.0 * sqrt( $x*$x + $y*$y );
  8. return $dist;
  9. }
  10. $db = file('db.txt');
  11. $sum_of_lat = 0;
  12. $sum_of_long = 0;
  13. $sum_of_time = 0;
  14. $array_time = array();
  15. $average = new stdClass;
  16. $average->latitude = 0;
  17. $average->longitude = 0;
  18. $average->inTime = 0;
  19. $average->parada = '';
  20. $db_size = count($db);
  21. $distances=[];
  22. $paradas = array(
  23. "Generales" => array(18.404916, -66.044828),
  24. "Natu_1" => array(18.403663, -66.045529),
  25. "Natu_2" => array(18.403769, -66.046572),
  26. "Centro" => array( 18.403898, -66.048255),
  27. "Biblio" => array(18.403994, -66.049781),
  28. "Merenderos" => array(18.406345, -66.050197),
  29. "Leyes" => array (18.406069, -66.047733),
  30. "Complejo" => array(18.406503, -66.046121),
  31. "ROTC_Entrada" => array(18.406083, -66.043605),
  32. "ROTC_Estacionamiento" => array( 18.407121, -66.042053)
  33. );
  34. for ($i = 0; $i < $db_size; $i++)
  35. {
  36. $decoded_location = json_decode($db[$i]);
  37. $sum_of_lat += $decoded_location->latitude;
  38. $sum_of_long += $decoded_location->longitude;
  39. array_push($array_time,$decoded_location->inTime);
  40. if($i+1 == $db_size)
  41. {
  42. $average->latitude = $sum_of_lat/$db_size;
  43. $average->longitude = $sum_of_long/$db_size;
  44. $average->inTime = date("h:i:s a",(array_sum(array_map('strtotime',$array_time)))/$db_size);
  45. }
  46. }
  47. foreach($paradas as &$thing)
  48. {
  49. $plo = distanceMeters($average->latitude, $average->longitude, $thing[0], $thing[1]);
  50. array_push($distances,$plo);
  51. //echo $distances;
  52. }
  53. //echo $encoded_average;
  54. $indice = array_search(min($distances),$distances);
  55. $checking = array_keys($paradas);
  56. //$encoded_average = json_decode($encoded_avarage);
  57. $average->parada = $checking[$indice];
  58. $encoded_average =json_encode($average);
  59. echo $encoded_average;
  60. //echo "pol";
  61. //echo $average;
  62. ?>