Ver Mensaje Individual
  #17 (permalink)  
Antiguo 04/07/2013, 07:50
carlosuc99
 
Fecha de Ingreso: junio-2011
Mensajes: 342
Antigüedad: 12 años, 10 meses
Puntos: 5
Exclamación Respuesta: Google maps y for PHP

Bueno, bueno... Muchas gracias a todos!!! Especialmente a @quinqui por su incondicional ayuda.

Ya lo he arragleda. Parte de los problemas eran los query. Pero las polyline tampoco estaban bien. Al tener el mismo nombre sólo quedaba la última polyline para el último marker así que les he dado un nombre único. No me ha hecho falta dárselos mediante un array. Si no que simplemente les añadí el contenido de una variable.

Este es el código final de mi mapa:

Código PHP:
Ver original
  1. <?php
  2. include('db_login.php');
  3.      
  4.       $connection = mysql_connect($db_host, $db_username, $db_password);
  5.       if (!$connection){
  6.       die ("Eror connecting database: <br/>". mysql_error());
  7.       }
  8.    
  9.       $db_select = mysql_select_db($db_database);
  10.       if (!$db_select){
  11.       die ("Database Error: <br/>". mysql_error());
  12.       }
  13.  
  14. $query = mysql_query("SELECT * FROM routes");
  15.  
  16. ?>
  17. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
  18. <style type="text/css">
  19.   html { height: 100% }
  20.   body { height: 100%; margin: 0px; padding: 0px }
  21.   #map_canvas { height: 100% }
  22. </style>
  23. <script type="text/javascript"
  24.     src="http://maps.google.com/maps/api/js?sensor=false&amp;language=es">
  25. </script>
  26. <script type="text/javascript">
  27.  
  28.  
  29. window.onload = function () {
  30.   var options = {
  31.     zoom: 5,
  32.     center: new google.maps.LatLng(40.84706, -2.944336),
  33.     mapTypeId: google.maps.MapTypeId.ROADMAP
  34.   };
  35.  
  36.   map = new google.maps.Map(document.getElementById('map_canvas'), options);
  37.  
  38.  
  39.   <?php
  40.         for($i = 0; $i < mysql_num_rows($query); $i++){
  41.             $icao = mysql_result($query, $i, 'from');
  42.             $query2 = mysql_query("SELECT * FROM airports WHERE icao='$icao'");
  43.             $latitude = mysql_result($query2, 0, 'latitude');
  44.             $longitude = mysql_result($query2, 0, 'longitude');
  45.             $city = mysql_result($query2, 0, 'city');
  46.         ?>
  47.   var Airport1 = '<h3 align="center" style="font-family:Arial, Helvetica, sans-serif"><?php echo $icao; ?> - <?php echo $city; ?></h3>';
  48.   var image = 'http://i46.tinypic.com/33zbm09.png';
  49.   var latLonCenter = new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>);
  50.   marker<?php echo $icao; ?> = new google.maps.Marker({
  51.     position: latLonCenter,
  52.     map: map,
  53.     draggable: false,
  54.     icon: image,
  55.     title: '<?php echo $icao; ?> - <?php echo $city; ?>',
  56.     Airport1: Airport1
  57.   });
  58.  
  59.     var infowindow<?php echo $icao; ?> = new google.maps.InfoWindow({
  60.     content: Airport1
  61.     });
  62.    
  63.     <?php
  64.     $query4 = mysql_query("SELECT * FROM routes WHERE `from`='$icao'");
  65.    
  66.    
  67.     for($y = 0; $y < mysql_num_rows($query4); $y++){
  68.     $destination = mysql_result($query4, $y, 'to');
  69.     $query5 = mysql_query("SELECT * FROM airports WHERE icao='$destination'");
  70.     $latitude_destination = mysql_result($query5, 0, 'latitude');
  71.     $longitude_destination = mysql_result($query5, 0, 'longitude');
  72.     ?>
  73.    
  74.     var PolyLine<?php echo $destination;?> = new google.maps.Polyline({
  75.         strokeColor: "#FF0000",
  76.         strokeOpacity: 2.0,
  77.         strokeWeight: 2
  78.     });
  79.  
  80.     var polyCords<?php echo $destination;?> = [
  81.     new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>),
  82.     new google.maps.LatLng(<?php echo $latitude_destination; ?>, <?php echo $longitude_destination; ?>)
  83.     ];
  84.    
  85.  
  86.     google.maps.event.addListener(marker<?php echo $icao; ?>, 'click', function() {
  87.     <?php for($z = 0; $z < mysql_num_rows($query); $z++){
  88.     $destination_delete = mysql_result($query, $z, 'to');?>
  89.    
  90.     PolyLine<?php echo $destination_delete;?>.setMap(null);
  91.    
  92.     <?php } ?>
  93.  
  94.     PolyLine<?php echo $destination;?>.setPath(polyCords<?php echo $destination;?>);
  95.  
  96.     PolyLine<?php echo $destination;?>.setMap(map);
  97.     });
  98.    
  99.     google.maps.event.addListener(marker<?php echo $icao; ?>, 'click', function () {
  100.       var n = 1;
  101.       var infowindow<?php echo $icao; ?> = new google.maps.InfoWindow({
  102.         content: "",
  103.         maxWidth: 320,  
  104.         zIndex: n
  105.       });
  106.      
  107.       infowindow<?php echo $icao; ?>.setContent(this.Airport1);
  108.       infowindow<?php echo $icao; ?>.setZIndex(n++);
  109.       infowindow<?php echo $icao; ?>.open(map, marker<?php echo $icao; ?>);
  110.     });
  111.  
  112. <?php } } ?>
  113.  
  114.  
  115.  
  116. }
  117. </script>
  118. </head>  
  119. <body>
  120. <center><div id="map_canvas" style="width:850px; height:560px;"></div></center>
  121. </body>