<?php
// ab12karte.php - Welches ist die nächstgelegene Haltestelle (Luftlinie) zu einer auszuwählenden Sehenswürdigkeit?

include 'header.php';


// ############ START QUERY ############################################################
if(isset($_POST['sight'])) {
   $sel_sight = $_POST['sight'];
} else {
   $sel_sight = '';
}


$abfrage = "
   SELECT sights.latitude AS 'si_lat', sights.longitude AS 'si_lng', sights.name AS 'si_name', stations.latitude AS 'st_lat', stations.longitude AS 'st_lng', stations.name AS 'st_name', 
   ROUND(ACOS(SIN(sights.latitude*PI()/180)*SIN(stations.latitude*PI()/180)+COS(sights.latitude*PI()/180)
   *COS(stations.latitude*PI()/180)*COS(stations.longitude*PI()/180 - sights.longitude*PI()/180))/(2*PI())*40000,3) AS 'dist'
   FROM stations, sights
   WHERE sights.id = $sel_sight
   ORDER BY dist
   LIMIT 1;
   ";
$ergebnis = mysqli_query($db, $abfrage);

$si_lat = 31.195501;
$si_lng = 121.47377;
$st_lat = 31.195501;
$st_lng = 121.47377;
$si_name = "";
$st_name = "";
 
 if ($ergebnis) {
   $objekt = mysqli_fetch_object($ergebnis);
   $dist = "$objekt->dist";
   $si_lat = "$objekt->si_lat";
   $si_lng = "$objekt->si_lng";
   $st_lat = "$objekt->st_lat";
   $st_lng = "$objekt->st_lng";
   $si_name = "$objekt->si_name";
   $st_name = "$objekt->st_name";
} 


$zoom = "14";

echo '<script type="text/javascript">';
echo '
   function load() {
      function createListener (marker, text) {
         GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(text);
         }
        );
      }
      if (GBrowserIsCompatible()) {
         var map = new GMap2(document.getElementById("map"));
		 map.setMapType(G_SATELLITE_MAP);
         map.addControl(new GLargeMapControl());
         map.addControl(new GMapTypeControl());
         map.addControl(new GScaleControl());
         map.addControl(new GOverviewMapControl());
         var icon_si = new GIcon();
         icon_si.image = "images/markersight.png";
         icon_si.shadow = "images/markerstation_schatten.png";
         icon_si.iconSize = new GSize(20, 20);
         icon_si.shadowSize = new GSize(20, 20);
         icon_si.iconAnchor = new GPoint(10, 10);
         icon_si.infoWindowAnchor = new GPoint(10, 10);
         var icon_st = new GIcon();
         icon_st.image = "images/markerstation.png";
         icon_st.shadow = "images/markerstation_schatten.png";
         icon_st.iconSize = new GSize(20, 20);
         icon_st.shadowSize = new GSize(20, 20);
         icon_st.iconAnchor = new GPoint(10, 10);
         icon_st.infoWindowAnchor = new GPoint(10, 10);		 
';
echo '
         map.setCenter(new GLatLng('.$si_lat.', '.$si_lng.'), '.$zoom.');
		 var point_si = new GLatLng('.$si_lat.','.$si_lng.');
		 var point_st = new GLatLng('.$st_lat.','.$st_lng.');
         var text_si = "'.$si_name.'";
		 var text_st = "'.$st_name.'";
         var marker_si = new GMarker(point_si, icon_si);
         var marker_st = new GMarker(point_st, icon_st);
         map.addOverlay(marker_si);
		 map.addOverlay(marker_st);
         createListener (marker_si, text_si);
         createListener (marker_st, text_st);
';
echo '
      }
    }
    </script>
';



echo '</head>';
// ############ START BODY ############################################################
if($sel_sight == "") {
   echo '<body>';
}
else {
   echo '<body onLoad="load()" onUnload="GUnload()">';
}

include 'menu.php';


// ############ START CONTENT############################################################
echo '
   <div id="content">
   <a href="abfragen.htm" class="menulink">zur&uuml;ck zu den Abfragen</a>
';   

echo '<h1>Welches ist die n&auml;chstgelegene Haltestelle (Luftlinie) zu einer auszuw&auml;hlenden Sehensww&uuml;rdigkeit?</h1>';


echo "<form method=\"post\" action=\"ab12karte.php\">";
echo '<select name="sight" size="1">
   <option value="1">The Tube
   <option value="2">Oriental Pearl Tower
   <option value="3">Jade Buddha Temple
   <option value="4">Longhua Pagoda / Temple
   <option value="5">Yuyuan Garden
   <option value="6">Shanghai Museum
   <option value="7">Science  and Technology Museum
   <option value="8">History Museum
   <option value="9">Art Museum
   <option value="10">Xujiahui Cathedral
   <option value="11">Ocean Aquarium
   <option value="12">Zoological Park
   <option value="13">Wusa Monument
   <option value="14">Century Plaza
   <option value="15">Songqingling\'s Mausoleum
   <option value="16">Natural History Museum
   </select>
';
echo '&nbsp;<input type="submit" name="form_linie" value="Sehens&uuml;rdigkeiten ausw&auml;hlen">';
echo '</form>';



if ($sel_sight =='') {
   echo "";
}	
else {

   echo "Die Nächste Haltestelle zu <b>$si_name</b> ist <b>$st_name</b>. Die Entfernung beträgt: <b>$dist</b> km";
   echo	'<div id="map" align="center" style="width: 800px; height: 400px"></div>'; 
}


include 'footer.php';
?>