2011-02-08 6 views
2

J'ai quelques problèmes avec google maps api (v3) et InfoWindows pas d'ouverture sur l'événement click du marqueur auquel ils sont attachés. Lorsque je débogue le javascript, les marqueurs et InfoWindows semblent avoir été créés correctement, donc je suppose que je fais quelque chose de mal en ajoutant l'écouteur d'événement click.Google maps InfoWindow ne s'ouvre pas lors d'un événement click

Ci-dessous se trouve l'ajout de marqueurs, etc. Quelqu'un peut-il voir quel peut être le problème?

$.post("/Club/SearchClubsByLocation", { latitude: searchLat, longitude: searchLng }, 
      function (clubs) { 
       $.each(clubs, function (i, club) { 
        var LL = new google.maps.LatLng(club.Latitude, club.Longitude); 
        pointArray.push(LL); 

        var infoWindow = new google.maps.InfoWindow({ 
         content: club.ClubName + " HELLO!!!" 
        }); 

        var marker = new google.maps.Marker({ 
         map: map, 
         position: LL 
        }); 

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

        markerArray.push(marker); 
       }); 

       for (i in pointArray) { 
        bounds.extend(pointArray[i]); 
       } 

       map.fitBounds(bounds); 
       pointArray = []; 
      }, 
      "json" 
     ); 

Merci pour toutes suggestions,

riches

Répondre

10

J'ai finalement résolu dans le en ayant une fonction en dehors de la fonction de poste qui crée un InfoWindow et ajoute un écouteur de clic à un marqueur pour ouvrir la InfoWindow

$.post("/Club/SearchClubsByLocation", { latitude: searchLat, longitude: searchLng }, 
    function (clubs) { 
    $.each(clubs, function (i, club) { 
     var LL = new google.maps.LatLng(club.Latitude, club.Longitude); 
     pointArray.push(LL); 

     var marker = new google.maps.Marker({ 
     map: map, 
     position: LL 
     }); 

     addInfoWindow(marker, club.ClubName); 

     markerArray.push(marker); 
    }); 

    for (i in pointArray) { 
     bounds.extend(pointArray[i]); 
    } 

    map.fitBounds(bounds); 
    pointArray = []; 
    }, 
    "json" 
); 

function addInfoWindow(marker, content) { 
    var infoWindow = new google.maps.InfoWindow({ 
     content: content 
    }); 

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

Il semble que vous avez besoin d'une fonction distincte pour ajouter plusieurs InfoWindows comme on le voit ici

Event Closures

Vive

Rich

+0

Merci pour poster votre réponse! Cela m'a beaucoup aidé. –

+0

Bonjour Rich. Je suis confronté à un problème similaire et j'ai essayé votre solution mais malheureusement, cela ne fonctionne pas. Cela vous dérangerait-il de jeter un coup d'œil? [Ma question est ici] (http://stackoverflow.com/questions/35456293/google-map-infowindow-not-opening). Merci beaucoup! –