2011-02-08 131 views
2

我有一些问题谷歌地图api(v3)和InfoWindows没有打开标记的点击事件,他们连接到。当我调试JavaScript标记和InfoWindows看起来已经正确创建,所以我假设我在添加单击事件侦听器时做错了什么。谷歌地图信息窗口没有打开点击事件

下面是添加标记等的情况。任何人都可以看到问题可能是什么?

$.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" 
     ); 

感谢您的任何建议,

丰富

回答

10

我终于解开了它在具有后函数创建一个信息窗口,并增加了一个点击监听到一个标记,以打开外部的功能信息窗口

$.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); 
    }); 
} 

看来你需要一个单独的功能来添加多个信息窗口,如下

看到个

Event Closures

干杯

丰富

+0

感谢张贴你的答案!这对我帮助很大。 – 2013-05-16 20:58:45