<?php
// ab7karte.php -  Wie gro&szlig; ist die Entfernung (Luftlinie) zwischen zwei zu w&auml;hlenden Sehensw&uuml;rdigkeiten?

include 'header.php';


// ############ START QUERY ############################################################
if(isset($_POST['sight']) && isset($_POST['sight2'])) {
   $gew_sight = $_POST['sight'];
   $gew_sight2 = $_POST['sight2'];
} else {
   $gew_sight = '';
   $gew_sight2 = '';
}
   
$query = "SELECT a.latitude AS 'a_lat', a.longitude AS 'a_lng', a.name AS 'a_name', b.latitude AS 'b_lat', b.longitude AS 'b_lng', b.name AS 'b_name',
   ROUND(ACOS(SIN(a.latitude*PI()/180)*SIN(b.latitude*PI()/180)+COS(a.latitude*PI()/180)
   *COS(b.latitude*PI()/180)*COS(b.longitude*PI()/180 - a.longitude*PI()/180))/(2*PI())*40000,3) AS 'dist'
   FROM sights a, sights b
   WHERE a.name = '$gew_sight' AND b.name = '$gew_sight2'
   ORDER BY dist
   LIMIT 1;
   ";
$result = mysqli_query($db, $query);
if ($result) {
   $objekt = mysqli_fetch_object($result);

   if($gew_sight =='' OR $gew_sight2 =='') {
      $a_lat = 31.195501;
      $a_lng = 121.47377;
	  $b_lat = 31.195501;
      $b_lng = 121.47377;
	  $a_name = "";
      $b_name = "";
   }
   else {
      $dist = "$objekt->dist";
	  $a_lat = "$objekt->a_lat";
      $a_lng = "$objekt->a_lng";
	  $b_lat = "$objekt->b_lat";
      $b_lng = "$objekt->b_lng";
	  $a_name = "$objekt->a_name";
      $b_name = "$objekt->b_name";
   }
} 
else {
   echo "Fehler";
}

$zoom = "12";
mysqli_free_result($result);
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 = new GIcon();
         icon.image = "images/markersight.png";
         icon.shadow = "images/markerstation_schatten.png";
         icon.iconSize = new GSize(20, 20);
         icon.shadowSize = new GSize(20, 20);
         icon.iconAnchor = new GPoint(10, 10);
         icon.infoWindowAnchor = new GPoint(10, 10);
';
echo '
         map.setCenter(new GLatLng('.$a_lat.', '.$a_lng.'), '.$zoom.');
		 var point_a = new GLatLng('.$a_lat.','.$a_lng.');
		 var point_b = new GLatLng('.$b_lat.','.$b_lng.');
         var text_a = "'.$a_name.'";
		 var text_b = "'.$b_name.'";
         var marker_a = new GMarker(point_a, icon);
         var marker_b = new GMarker(point_b, icon);
         map.addOverlay(marker_a);
		 map.addOverlay(marker_b);
         createListener (marker_a, text_a);
         createListener (marker_b, text_b);
';
echo '
      }
    }
    </script>
';



echo '</head>';
// ############ START BODY ############################################################
if($gew_sight =='' OR $gew_sight2 =='' OR $gew_sight == $gew_sight2) {
   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>Wie gro&szlig; ist die Entfernung (Luftlinie) zwischen zwei zu w&auml;hlenden Sehensw&uuml;rdigkeiten?</h1>';


echo "<form method=\"post\" action=\"ab7karte.php\">";
echo '<select name="sight" size="1">
   <option>The Tube
   <option>Oriental Pearl Tower
   <option>Jade Buddha Temple
   <option>Longhua Pagoda / Temple
   <option>Yuyuan Garden
   <option>Shanghai Museum
   <option>Science  and Technology Museum
   <option>History Museum
   <option>Art Museum
   <option>Xujiahui Cathedral
   <option>Ocean Aquarium
   <option>Zoological Park
   <option>Wusa Monument
   <option>Century Plaza
   <option>Songqingling\'s Mausoleum
   <option>Natural History Museum
   </select>
';
echo '<select name="sight2" size="1">
   <option>The Tube
   <option>Oriental Pearl Tower
   <option>Jade Buddha Temple
   <option>Longhua Pagoda / Temple
   <option>Yuyuan Garden
   <option>Shanghai Museum
   <option>Science  and Technology Museum
   <option>History Museum
   <option>Art Museum
   <option>Xujiahui Cathedral
   <option>Ocean Aquarium
   <option>Zoological Park
   <option>Wusa Monument
   <option>Century Plaza
   <option>Songqingling\'s Mausoleum
   <option>Natural History Museum
   </select>
';
echo '&nbsp;<input type="submit" name="form_linie" value="Sehens&uuml;rdigkeiten ausw&auml;hlen">';
echo '</form>';

if ($gew_sight =='' OR $gew_sight2 =='') {
   echo "";
}
else {

   if ($gew_sight == $gew_sight2) {
      echo 'Bitte w&auml;hlen Sie zwei verschiedene Sehens&uuml;rdigkeiten!';
   }
   else {
      echo "Die Entfernung zwischen <b>$gew_sight</b> und <b>$gew_sight2</b> betr&auml;gt: <b>$dist</b> km";
      echo '<div id="map" align="center" style="width: 800px; height: 400px"></div>';  
   }
}
include 'footer.php';
?>