Services_OpenStreetMap
[ class tree: Services_OpenStreetMap ] [ index: Services_OpenStreetMap ] [ all elements ]

Source for file searchNear.php

Documentation is available at searchNear.php

  1. <script>
  2. jQuery.each(window.mapMarkers, function() {
  3.     window.map.removeLayer(this);
  4. });
  5. </script>
  6. <?php
  7. require_once 'Services/OpenStreetMap.php';
  8.  
  9. const KILOMETERS = 6372.8;
  10. const MILES = 3963.1676;
  11. const NAUTUCAL_MILES = 3443.89849;
  12. /**
  13.  * Given two sets of lat/lon pairs, calculate the distance between them
  14.  *
  15.  * Distance defaults to being calculated in kilometers.
  16.  *
  17.  * @param mixed $aLon 
  18.  * @param mixed $aLat 
  19.  * @param mixed $bLon 
  20.  * @param mixed $bLat 
  21.  * @param mixed $unit 
  22.  *
  23.  * @return float 
  24.  */
  25. function calculateDistance($aLon$aLat$bLon$bLat$unit = KILOMETERS)
  26. {
  27.     $sinHalfDeltaLat sin(deg2rad($bLat $aLat/ 2.0);
  28.     $sinHalfDeltaLon sin(deg2rad($bLon $aLon/ 2.0);
  29.     $lonSqrd $sinHalfDeltaLon $sinHalfDeltaLon;
  30.     $latSqrd $sinHalfDeltaLat $sinHalfDeltaLat;
  31.     $angle = 2 * asin(
  32.         sqrt($latSqrd cos(deg2rad($aLat)) cos(deg2rad($bLat)) $lonSqrd)
  33.     );
  34.     return $unit $angle;
  35. }
  36.  
  37. /**
  38.  * sortByDistance
  39.  *
  40.  * @return function 
  41.  */
  42. function sortByDistance()
  43. {
  44.     return function ($a$b{
  45.         if ($a->distance == $b->distance{
  46.             return 0;
  47.         }
  48.         return ($a->distance < $b->distance? -1 : 1;
  49.     };
  50. }
  51.  
  52. $lat = null;
  53. $lon = null;
  54. $k = null;
  55. $v = null;
  56.  
  57. if (isset($_GET['q'])) {
  58.     $v strpos($_GET['q']'|');
  59.     if ($v != false{
  60.         list($k$vexplode('|'$_GET['q']);
  61.         $k trim($k);
  62.         $v trim($v);
  63.     else {
  64.         die();
  65.     }
  66. }
  67.  
  68. if (isset($_GET['lat'])) {
  69.     $lat $_GET['lat'];
  70. }
  71. if (isset($_GET['lat'])) {
  72.     $lon $_GET['lon'];
  73. }
  74. $osm = new Services_OpenStreetMap();
  75. $osm->loadXML("./map.osm");
  76. $results $osm->search(array($k => $v));
  77. echo "List of $k/$v\n";
  78. echo "==================\n\n";
  79.  
  80. foreach ($results as $result{
  81.     if ($result->getType(== 'node'{
  82.         $bLat $result->getLat();
  83.         $bLon $result->getLon();
  84.     elseif ($result->getType(== 'way' && $result->isClosed()) {
  85.         $nodes $result->getNodes();
  86.         array_pop($nodes);
  87.         $bLat = 0;
  88.         $bLon = 0;
  89.         foreach ($nodes as $node{
  90.             $n $osm->getNode($node);
  91.             if ($n !== false{
  92.                 $bLat += $n->getLat();
  93.                 $bLon += $n->getLon();
  94.             }
  95.         }
  96.         $bLat $bLat sizeof($nodes);
  97.         $bLon $bLon sizeof($nodes);
  98.     }
  99.     $distance calculateDistance($lat$lon$bLat$bLon);
  100.     // $distance = $distance * 1000; // convert to metres
  101.     $result->distance = $distance;
  102.     $result->lat = $bLat;
  103.     $result->lon = $bLon;
  104. }
  105.  
  106. usort($resultssortByDistance());
  107. foreach ($results as $result{
  108.     $tags $result->getTags();
  109.     $name $tags['name'];
  110.     $addrStreet $tags['addr:street'];
  111.     $addrCity $tags['addr:city'];
  112.     $addrCountry $tags['addr:country'];
  113.     $addrHouseName $tags['addr:housename'];
  114.     $addrHouseNumber $tags['addr:housenumber'];
  115.     $openingHours $tags['opening_hours'];
  116.     $phone $tags['phone'];
  117.     $bLat $result->lat;
  118.     $bLon $result->lon;
  119.     $oh->setValue($openingHours);
  120.     $open $oh->isOpen();
  121.  
  122.     $line1 ($addrHouseNumber$addrHouseNumber $addrHouseName;
  123.     if ($line1 != null{
  124.         $line1 .= ', ';
  125.     }
  126.     echo  "$name\n";
  127.     $distance $result->distance;
  128.     echo "$bLat"$bLon" ("number_format($distance4)"km)\n";
  129.     echo "<script>
  130.         var marker = window.L.marker([",$bLat  ,", ",$bLon," ]);
  131.         marker.addTo(window.map);
  132.     ";
  133.     echo "marker.bindPopup(\"<b>"htmlspecialchars($name)"</b>\");";
  134.     echo "window.mapMarkers.push(marker);";
  135.     echo "</script>";
  136.     if ($line1 != null && $addrStreet != null{
  137.         echo "{$line1}{$addrStreet}\n";
  138.     }
  139.     if ($phone != null{
  140.         echo "<a href='tel:$phone'>$phone</a>\n";
  141.     }
  142.     if ($openingHours !== null{
  143.         echo  "$openingHours\n";
  144.         echo "Open?: ";
  145.         if ($open === null{
  146.             echo "Maybe\n";
  147.         elseif ($open == true{
  148.             echo "Yes\n";
  149.         else {
  150.             echo "No\n";
  151.         }
  152.     }
  153.     echo "\n\n";
  154. }
  155.  
  156. $result $results[0];
  157. $bLat $result->lat;
  158. $bLon $result->lon;
  159. echo "<script>";
  160. echo "window.map.panTo([",$bLat,", ",$bLon," ]);";
  161. echo "</script>";

Documentation generated on Tue, 28 May 2019 19:19:08 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.