Ver Mensaje Individual
  #19 (permalink)  
Antiguo 05/12/2012, 10:20
Avatar de maximendez88
maximendez88
 
Fecha de Ingreso: septiembre-2012
Ubicación: Montevideo
Mensajes: 131
Antigüedad: 11 años, 7 meses
Puntos: 3
Sonrisa Respuesta: unir marcadores con google maps

Código HTML:
Ver original
  1. <!DOCTYPE html>
  2. <meta charset="utf-8" />
  3.  <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
  4. <title>Test Google Maps</title>
  5. </head>
  6.     <!-- MAPA -->
  7.     <div id="map_canvas" style="width: 600px; height: 600px;"></div>
  8.     <input type="text" name="ciudad" id="ciudad" /> <button onclick="agregarCiudad();">Agregar</button>
  9.    
  10.     <!-- funcion para agregar ciudad. La separo porque es sólo ejemplo -->
  11.     <script type="text/javascript">
  12.         function agregarCiudad(){
  13.             showAddress(document.getElementById('ciudad').value);
  14.             document.getElementById('ciudad').value = '';
  15.         }
  16.     </script>
  17.    
  18.     <script type="text/javascript">
  19.     //variables y objetos globales
  20.     var map             //mapa
  21.     var geocoder;       //geocoder
  22.     var _path = [];     // ruta vacía
  23.     var _ciudades = new Array(); // ciudades que se cargaron para guardar
  24.  
  25.    
  26.     var _poliLinea = new google.maps.Polyline({ //polilinea
  27.         path: _path,
  28.         strokeColor: '#FF0000',
  29.         strokeOpacity: 1.0,
  30.         strokeWeight: 2
  31.     });
  32.  
  33.     //inicializo
  34.     initialize();
  35.  
  36.     function initialize() {
  37.         //centrado
  38.         var myLatLng = new google.maps.LatLng(37.4419, -122.1419);
  39.         //opciones
  40.         var mapOptions = {
  41.             zoom: 1,
  42.             center: myLatLng,
  43.             mapTypeId: google.maps.MapTypeId.TERRAIN
  44.         };
  45.         //geocoder
  46.         geocoder = new google.maps.Geocoder();
  47.         //creo mapa
  48.         map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
  49.        
  50.         //asigno polilinea al mapa
  51.         _poliLinea.setMap(map);
  52.  
  53.         //verifico si hay ciudades guardadas en localStorage
  54.         if(localStorage['ciudades']){
  55.             __ciudad = JSON.parse(localStorage['ciudades']);
  56.             //agrego cada ciudad que se guardó
  57.             for(i=0;i<__ciudad.length;i++){
  58.                showAddress(__ciudad[i]);
  59.            }
  60.        }
  61.  
  62.  
  63.    }
  64.  
  65.    function showAddress(s_address) {
  66.        var address = s_address;
  67.        //busco la direccion dada
  68.        geocoder.geocode( { 'address': address}, function(results, status) {
  69.          if (status == google.maps.GeocoderStatus.OK) {
  70.            //centro el mapa en el resultado
  71.            map.setCenter(results[0].geometry.location);
  72.            //creo marcador
  73.            var marker = new google.maps.Marker({
  74.                map: map,
  75.                position: results[0].geometry.location
  76.            });
  77.            //agrego punto a la linea
  78.            agregarRuta(results[0].geometry.location)
  79.            
  80.            //guardo la ciudad en LocalStorage
  81.            _ciudades.push(results[0].formatted_address);
  82.            localStorage['ciudades'] = JSON.stringify(_ciudades);
  83.  
  84.          } else {
  85.            alert('Error: ' + status);
  86.          }
  87.        });
  88.     }
  89.    
  90.    function agregarRuta(o_punto){
  91.  
  92.        //creo punto a partir de la dirección dada
  93.        var o_LL = new google.maps.LatLng(o_punto.lat(), o_punto.lng())
  94.        //agrego el punto al array de la ruta
  95.        _path.push(o_LL);
  96.        //cargo la nueva ruta en la polilinea
  97.        _poliLinea.setPath(_path);
  98.        //centro el mapa en el último punto
  99.        map.setCenter(o_LL);
  100.        //Hago un poco de zoom
  101.        map.setZoom(6);
  102.    }
  103.  
  104.    </script>
  105. </body>
  106. </html>

bueno, he estado utilizando el código que me pasaste...localmente me funciono, solo que cuando lo pruebo en mi servidor no funciona... cada vez que actualizo el navegador pierdo la ruta que fui marcando...