2011-02-08 177 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