Pregunta

crear varios GMarkers (de datos JSON cargados por la función de "carga" jQuery), en todos ellos agrego un detector de eventos para abrir el objeto ventana de información que he creado antes en el marcador, y luego a todos añadir al mapa .

El problema es que la ventana de información se abre siempre en el mismo marcador. todo lo que tenía este trabajo antes, no veo dónde está el problema ... ámbito de la variable? estúpida en algún error?

ejemplo , y aquí es un acceso directo al archivo javascript

El código:

    var map;
    var infowindow;
    $(document).ready(function() {

        var myLatlng = new google.maps.LatLng(47.15984,2.329102);
      var myOptions = {
        zoom: 6,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.HYBRID,
        scrollwheel: false
      }

      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

      infowindow = new google.maps.InfoWindow({content:'<p>Test</p>'});

        $.getJSON("data.json", function(data) {

            var markers = [];
            for (var i = data.length - 1; i >= 0; i--){
                var latLng = new google.maps.LatLng(data[i].lat, data[i].lng);
              var marker = new google.maps.Marker({position: latLng});

              google.maps.event.addListener(marker, 'click', function() {
                    infowindow.open(map,marker);
              });

              markers.push(marker);
            };

            for (var j = markers.length - 1; j >= 0; j--){
                markers[j].setMap(map);
            };

        });
    });
¿Fue útil?

Solución

Cambiar

infowindow.open(map,marker);

a

infowindow.open(map,this);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top