Google Maps Spiderfying Markers&添加听众

时间:2013-06-19 17:38:13

标签: javascript google-maps-api-3 listener google-maps-markers infowindow

我正在尝试Spiderfy我的标记并向他们添加听众。 Spidefying工作得很好,这意味着我得到重叠的标记蜘蛛侠,但问题是我似乎无法想象如何为这些标记添加侦听器。换句话说,我尝试过以下方法:

这是我的代码:

var iw = new google.maps.InfoWindow();
var oms = new OverlappingMarkerSpiderfier(carte, {keepSpiderfied:true});
oms.addListener('click', function(marker){
    iw.setContent(content);
    iw.open(carte, marker);
});
var markers =[];
var bounds = new google.maps.LatLngBounds();
if (response.length != 0) {
    for (var i = 0 ; i < response.length; i++) {
        var loc = new google.maps.LatLng(response[i].latlong[0], response[i].latlong[1]);
        bounds.extend(loc);
        var lemarqueur = new google.maps.Marker({
            position: loc,
            title: response[i].title
        });
        content = '<table><tr><td><img src="'
        +response[i].image+'"/></td><td><p style="font-size: 13px">'
        +response[i].title+'</p> <p style="font-size: 10px"><b>Artists:</b> '
        +response[i].artist+'<br><b>Date:</b> '+response[i].startDate+'<br>'
        +response[i].address.name +' '+response[i].address.street + '<br>'
        +response[i].address.postalcode +', '+response[i].address.city +', '
        +response[i].address.country
        +'<br><a target="_blank" href =' 
        +response[i].url+'>More info</a></p></td></tr></table>';

        oms.addMarker(lemarqueur);
        markers.push(lemarqueur);
    };
    carte.fitBounds(bounds);
    var markerCluster = new MarkerClusterer(carte, markers);
    markerCluster.setMaxZoom(15);
    markerCluster.setGridSize(40);
    google.maps.event.addDomListener(window, 'load', initialiser);

所以我想知道我应该把addListener()块放在哪里?

这是我之前在循环中使用的google.maps.event.addListener(),它运行良好:

google.maps.event.addListener(lemarqueur, 'click', function() {
    InfoWindow.open(carte, lemarqueur);
});

我希望这个问题足够清楚,
提前谢谢。

2 个答案:

答案 0 :(得分:4)

oms.addListener()google.maps.event.addListener()的行为方式不同。 所以我通过在google.maps.event.addListener()循环中使用原始oms.addListener()而不是for来使其工作。像这样:

var infoWindows = [];

function closeInfoWindows(){
    var i = infoWindows.length;
    while(i--){
        infoWindows[i].close();
    }
}

function newMarker(map, response, oms){
var loc = new google.maps.LatLng(response.latlong[0], response.latlong[1]);
var marker = new google.maps.Marker({
    position: loc,
    title: response.title
});
oms.addMarker(marker);
var WindowOptions = { content:'some desc...'};    
var InfoWindow = new google.maps.InfoWindow(WindowOptions);
infoWindows.push(InfoWindow);
google.maps.event.addListener(marker, 'click', function() {
    closeInfoWindows();
    InfoWindow.open(carte, marker); // or this instead of marker
});
return marker;
}


for (var i = 0 ; i < response.length; i++) {
        markers.push(newMarker(map, response[i], oms));
    };

额外信用: geocodezipAll infowindows have same data

可以在这里找到一个工作示例(我的应用):concert-dacote.com

答案 1 :(得分:0)

以下是使用 NO FOR LOOP NEEDED 将侦听器添加到 spiderfiered 标记的另一种方法:

"`r`n"