Ver Mensaje Individual
  #1 (permalink)  
Antiguo 01/06/2015, 08:18
IVICP3
 
Fecha de Ingreso: febrero-2015
Mensajes: 61
Antigüedad: 9 años, 2 meses
Puntos: 0
Marcadores de Google Maps

Buenas, estoy haciendo un codigo donde un usuario tiene que introducir lugares y se guardan en una base de datos.

Al cargar la pagina quiero que todos esos lugares esten plasmados como marcadores en un mapa de google maps, pero solo me muestra el primer lugar, los siguientes no, a ver si alguien me puede ayudar.

En este momento me da error en la linea 56, ya que no puedo leer un array en la api. Como le paso mas de 1 resultado para que lo lea la api? Y para mostrar en la variable de google maps "marker"? (Línea 106)

Gracias.


Código Javascript:
Ver original
  1. <?php
  2.                          
  3.                           $doc=new DOMDocument();
  4.                        
  5.  
  6.     $host = "localhost";
  7.     $user = "root";
  8.     $pass = "123456";
  9.     $bd = "facebook";
  10.     $dbc;
  11.  
  12.     $conexion = mysqli_connect($host, $user, $pass, $bd);
  13.  
  14.     if($conexion->connect_errno){
  15.       echo "ERROR AL CONECTARSE ($conexion->connect_errno)";
  16.     }
  17.  
  18.  
  19.  
  20.  
  21.               $usuario = $_SESSION['username'];
  22.  
  23.  
  24.                         $consulta="SELECT lugar FROM lugares WHERE user = '$usuario' ";
  25.  
  26.  
  27.                         $resultado = mysqli_query($conexion, $consulta) or die(mysql_error());
  28.                         $datos = mysqli_fetch_array($resultado);
  29.  
  30.  
  31.  
  32.                           $row = $resultado->num_rows;
  33.  
  34.                             if($row<>0){
  35.  
  36.                                 $direccion[] = $datos["lugar"];
  37.                                
  38.                               }else{
  39.                                 echo "Error";
  40.                               }
  41.  
  42.                              
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.                        
  55.                        
  56.                             $xml="http://maps.googleapis.com/maps/api/geocode/xml?address=".$direccion."&sensor=true";
  57.       //echo $xml;
  58.                             $doc->load($xml);
  59.  
  60.                             $persona=$doc->getElementsByTagName("result");
  61.  
  62.                             foreach ($persona as $p)
  63.                             {
  64.                                     $latitud=$p->getElementsByTagName("lat");
  65.                                     $latitud=$latitud->item(0)->nodeValue;
  66.                                    
  67.                                     $longitud=$p->getElementsByTagName("lng");
  68.                                     $longitud=$longitud->item(0)->nodeValue;
  69.                                    
  70.                                    
  71.                                    
  72.                                     //echo "latitud=".$latitud." - longitud=".$longitud."<br>";
  73.                             }
  74.  
  75.  
  76.  
  77.                            
  78.                           ?>
  79.  
  80.  
  81. <script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=true">
  82. </script>
  83. <script type="text/javascript">
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.    function cargar_mapa()
  92.     {
  93.         var myOptions = {
  94.           scaleControl: true,
  95.           center: new google.maps.LatLng(<?php echo $latitud;?>, <?php echo $longitud;?>),
  96.           zoom: 6,
  97.           mapTypeId: google.maps.MapTypeId.ROADMAP
  98.         };
  99.  
  100.         var map = new google.maps.Map(document.getElementById('map_canvas'),
  101.             myOptions);
  102.  
  103.  
  104.  
  105.  
  106. for (var i = 0, j = <?php.$direccion.?>.length; i < j; i++) {  
  107.   var contenido = direccion[i].contenido;
  108.   var marker = new google.maps.Marker({
  109.     position: new google.maps.LatLng(direccion[i].position.lat, direccion[i].position.lng),
  110.     map: map
  111.   });
  112.  
  113.  
  114.  
  115.  
  116.  
  117.        /* var marker = new google.maps.Marker({
  118.           map: map,
  119.           title: "MARCADOR",
  120.           position: map.center,
  121.           animation: google.maps.Animation.BOUNCE
  122.         });*/
  123.  
  124.         marker.setIcon('https://dl.dropboxusercontent.com/u/20056281/Iconos/male-2.png');
  125.  
  126.         var infowindow = new google.maps.InfoWindow();
  127.         infowindow.setContent('<b>Mi ubicacion</b>');
  128.         google.maps.event.addListener(marker, 'click', function() {
  129.             infowindow.open(map, marker);
  130.         });
  131.        
  132.       }
  133.  
  134.       google.maps.event.addDomListener(window, 'load', cargar_mapa);
  135.  
  136. </script>