/php-edition/chapter7/ServerClosest/server.php

https://github.com/iamamused/Apress-Beginning-Google-Maps-Applications · PHP · 107 lines · 58 code · 14 blank · 35 comment · 0 complexity · 637fd1a0c9ecabceaed1b2840be2880b MD5 · raw file

  1. <?php
  2. //surface distance calculation from Listing 7-3
  3. /* [listing 7-3] */
  4. function surfaceDistance($lat1,$lng1,$lat2,$lng2,$type='km'){
  5. $a1 = deg2rad($lat1); //lat 1 in radians
  6. $a2 = deg2rad($lat2); //lat 2 in radians
  7. $b1 = deg2rad($lng1); //lng 1 in radians
  8. $b2 = deg2rad($lng2); //lng 2 in radians
  9. // earth radius = 6378.8 kilometers or 3963 miles
  10. switch(strtolower($type)) {
  11. case 'km': $r = 6378.8; break; //kilometers
  12. case 'm': $r = 3963; break; //miles
  13. case 'n': $r = 3443.9; break; //nautical
  14. }
  15. return acos(cos($a1)*cos($b1)*cos($a2)*cos($b2)
  16. + cos($a1)*sin($b1)*cos($a2)*sin($b2)
  17. + sin($a1)*sin($a2)) * $r;
  18. }
  19. /* [listing 7-3 end] */
  20. //retrieve the variables from the GET vars
  21. list($knownLat,$knownLng) = explode(',',$_GET['known']);
  22. list($nelat,$nelng) = explode(',',$_GET['ne']);
  23. list($swlat,$swlng) = explode(',',$_GET['sw']);
  24. //clean the data
  25. $knownLat=(float)$knownLat;
  26. $knownLng=(float)$knownLng;
  27. $nelng=(float)$nelng;
  28. $swlng=(float)$swlng;
  29. $nelat=(float)$nelat;
  30. $swlat=(float)$swlat;
  31. //connect to the database
  32. require($_SERVER['DOCUMENT_ROOT'] . '/db_credentials.php');
  33. $conn = mysql_connect("localhost", $db_name, $db_pass);
  34. mysql_select_db("googlemapsbook", $conn);
  35. /*
  36. * Retrieve the points within the boundary of the map.
  37. * For the FCC data, all the points are within the US so we
  38. * don't need to worry about the meridian problem.
  39. *
  40. * NOTE: For purposes of the on-line example we've also limited
  41. * the data to only towers in Hawaii
  42. */
  43. /*
  44. //using a view
  45. $result = mysql_query(
  46. "SELECT
  47. longitude as lng,latitude as lat
  48. FROM
  49. fcc_towers
  50. WHERE
  51. struc_state='HI' AND
  52. (longitude > $swlng AND longitude < $nelng)
  53. AND (latitude <= $nelat AND latitude >= $swlat)
  54. ORDER BY
  55. lat");
  56. */
  57. //using joins...
  58. $result = mysql_query(
  59. "SELECT
  60. fcc_location.longitude as lng,fcc_location.latitude as lat
  61. FROM
  62. fcc_structure, fcc_owner, fcc_location
  63. WHERE
  64. unique_si=unique_si_own
  65. AND unique_si=unique_si_loc
  66. AND struc_state='HI'
  67. AND (longitude > $swlng AND longitude < $nelng)
  68. AND (latitude <= $nelat AND latitude >= $swlat)
  69. ORDER BY
  70. lat"
  71. , $conn);
  72. $list = $distanceList = array();
  73. $i=0;
  74. $row = mysql_fetch_assoc($result);
  75. while($row)
  76. {
  77. $i++;
  78. extract($row);
  79. $list[$i] = "p{$i}:{lat:{$lat},lng:{$lng}}";
  80. $distanceList[$i] = surfaceDistance($lat,$lng,$knownLat,$knownLng,'km');
  81. $row = mysql_fetch_assoc($result);
  82. }
  83. //sort the arrays by distance
  84. array_multisort($distanceList,$list);
  85. //free the distance list
  86. unset($distanceList);
  87. //slice the array to the desired number of points
  88. //20 in this case
  89. $list = array_slice($list,0,20);
  90. //echo back the JavaScript object
  91. header('content-type:text/plain;');
  92. echo "var points = {\n\t".join(",\n\t",$list)."\n}";
  93. ?>