<?
include ('common.inc');
/*
// coordinate punto 1
$lat1 = 38.168668;
$lng1 = 15.544298;
// coordinate punto 2
$lat2 = 38.1683010;
$lng2 = 15.5430460;
// calcolo la distanza
$distanza = calcola_distanza($lat1, $lng1, $lat2, $lng2);
foreach ($distanza as $unita => $valore) {
  echo $unita . ': ' . number_format($valore,1) . '<br/>';
}
*/
include ('conn.inc');
echo"<BR><u>ELENCO FERMATE ATM NEL RAGGIO DI 500 mt.</u><BR><BR>";
//query che estrae tutti i record entro 500 mt
$lat =risposta($_GET[lat]);
$lng =risposta($_GET[lon]);
echo"$lat,$lng";
$query=mysql_query("SELECT * FROM fermate WHERE TRUNCATE ( 6363 * sqrt( POW( RADIANS($lat) - RADIANS(latitudine) , 2 ) + POW( RADIANS($lng) - RADIANS(longitudine) , 2 ) ) , 3 ) < 0.5");
$num = mysql_num_rows($query);
$is=0;
while($is<$num)
{
	$rec=mysql_fetch_array($query);
	$numero=$rec[numero];
	$descrizione=$rec[descrizione];
	if ($descrizione=='')
		$descrizione='da definire';
	$lat2 = $rec[latitudine];
	$lng2 = $rec[longitudine];
  $theta = $lng - $lng2;
 // $miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
  $miles = (sin(deg2rad($lat)) * sin(deg2rad($lat2))) + (cos(deg2rad($lat)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)));
  $miles = acos($miles);
  $miles = rad2deg($miles);
  $miles = $miles * 60 * 1.1515;
  $feet = $miles * 5280;
  $yards = $feet / 3;
  $kilometers = $miles * 1.609344;
  $meters = number_format($kilometers * 1000,2);
	echo"<B>$numero</B> - $descrizione ($lat2, $lng2)  distanza mt. $meters<br>";
	$is++;
}
?>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<?php
//include('common.inc');
echo"
<script type=\"text/javascript\">
function load()
{
";//fine echo
echo"var sito0 = new google.maps.LatLng(".$lat.",".$lng.");\n\n";
$i=1;
	$getcoord=new Query("SELECT * FROM fermate WHERE TRUNCATE ( 6363 * sqrt( POW( RADIANS($lat) - RADIANS(latitudine) , 2 ) + POW( RADIANS($lng) - RADIANS(longitudine) , 2 ) ) , 3 ) < 0.5");
	while($recle=$getcoord->getNextRow())
	{
		$nomelg=fromdb($recle[numero]);
		$coord=fromdb($recle[latitudine]).','.fromdb($recle[longitudine]);
//		$idluogo=$recle[id];
		$idluogo=$i;
		echo"var sito$idluogo = new google.maps.LatLng($coord);\n\n";
		$i++;
	}
	$getcoord->riavvolgi();
	echo"
	var myMapOptions = {
	zoom: 16,
	center: sito0,
	mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	
	var map = new google.maps.Map(document.getElementById(\"map\"),myMapOptions);
	 var image = new google.maps.MarkerImage(
		'marker-images/image.png',
		new google.maps.Size(40,41),
		new google.maps.Point(0,0),
		new google.maps.Point(20,41)
	  );
	  var shadow = new google.maps.MarkerImage(
		'marker-images/shadow.png',
		new google.maps.Size(64,41),
		new google.maps.Point(0,0),
		new google.maps.Point(20,41)
	  );
	   var image0 = new google.maps.MarkerImage(
		'marker-images/image0.png',
		new google.maps.Size(40,41),
		new google.maps.Point(0,0),
		new google.maps.Point(20,41)
	  );
	  var shadow0 = new google.maps.MarkerImage(
		'marker-images/shadow.png',
		new google.maps.Size(64,41),
		new google.maps.Point(0,0),
		new google.maps.Point(20,41)
	  );
	
  var shape = {
    coord: [27,0,27,1,30,2,31,3,31,4,31,5,32,6,32,7,32,8,32,9,32,10,32,11,32,12,32,13,32,14,33,15,34,16,36,17,37,18,38,19,39,20,39,21,39,22,39,23,39,24,39,25,39,26,39,27,39,28,39,29,36,30,32,31,30,32,30,33,30,34,29,35,29,36,29,37,28,38,28,39,28,40,18,40,17,39,17,38,16,37,15,36,14,35,3,34,1,33,1,32,0,31,0,30,0,29,0,28,0,27,0,26,0,25,0,24,1,23,2,22,2,21,3,20,4,19,4,18,5,17,5,16,4,15,4,14,4,13,4,12,3,11,3,10,3,9,3,8,3,7,2,6,2,5,3,4,3,3,20,2,22,1,23,0,27,0],
    type: 'poly'
  };\n\n\n";//fine echo
	$i=1;
	echo"marker0 = new google.maps.Marker({draggable: false,raiseOnDrag: false,icon: image0,shadow0: shadow0,shape: shape,map: map,	position: sito0});\n \n";
	while($recle=$getcoord->getNextRow())
	{
		$nomelg=fromdb($recle[numero]);
		$coord=fromdb($recle[latitudine]).','.fromdb($recle[longitudine]);
		$idluogo=$i;
		echo"marker$idluogo = new google.maps.Marker({draggable: false,raiseOnDrag: false,icon: image,shadow: shadow,shape: shape,map: map,	position: sito$idluogo});\n \n";
		$i++;
	}
	$i=1;
	$getcoord->riavvolgi();
	echo"
		myInfoWindowOptions0 = {
				content: '<div class=\"info-window-content\"><font size=3 face=verdana><b>Tu sei QUI</b></font></div>', maxWidth: 95
			};
			infoWindow0 = new google.maps.InfoWindow(myInfoWindowOptions0);
			google.maps.event.addListener(marker0, 'mouseover', function() {
				infoWindow0.open(map,marker0);
			});
			google.maps.event.addListener(marker0, 'mouseout', function(){
				infoWindow0.close();
			});";
	while($recle=$getcoord->getNextRow())
	{
		$nomelg=addslashes(fromdb($recle[numero]));
		$deslg=addslashes(fromdb($recle[descrizione]));
		$coord=fromdb($recle[latitudine]).','.fromdb($recle[longitudine]);
		$idluogo=$i;
		echo"
		myInfoWindowOptions$idluogo = {
				content: '<div class=\"info-window-content\"><font size=3 face=verdana><b>Fermata n. $nomelg<br>$deslg</b></font></div>', maxWidth: 275
			};
			infoWindow$idluogo = new google.maps.InfoWindow(myInfoWindowOptions$idluogo);
			google.maps.event.addListener(marker$idluogo, 'mouseover', function() {
				infoWindow$idluogo.open(map,marker$idluogo);
			});
			google.maps.event.addListener(marker$idluogo, 'mouseout', function(){
				infoWindow$idluogo.close();
			});";
			$i++;
	}
	
	
  echo"var flightPlanCoordinates = [".$poly."];";
  echo"var flightPath = new google.maps.Polyline({
    path: flightPlanCoordinates,
    geodesic: true,
    strokeColor: '#FF0000',
    strokeOpacity: 1.0,
    strokeWeight: 5
  });
  
  flightPath.setMap(map);
  ";
echo"
}
</script>";
//fine echo
?>
</HEAD>
<BODY  onload="load()">
<center>
<table width=100%>
	<tr>
		<td bgcolor=#ECEDF1 align=center>
			<table width=100% bgcolor=#ffffff>
				<tr>
					<td colspan=2 ALIGN=CENTER>
						<table width=100% bgcolor=#ffffff>
							<tr>
								<td class=titolopagina colspan=2>:: FERMATE ATM nel raggio di 500 metri</td>
							</tr>
							<tr>
								<td colspan=2 valign=top>
									<table width=100% border=0 cellpadding=1 cellspacing=1>
									<tr>
										<td width=* valign=top>
											<div id="map" style="width:800px;height:800px; border:solid 1px #000000"></div>				
										</td>
									</tr>
									</table>
									
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>
<table width=990 border=0 align=center>
</table>
</center>
</BODY>
</HTML>