如何关闭所有打开的标记?

时间:2011-09-23 14:09:51

标签: javascript google-maps google-maps-api-3

我有这段代码:

var markers = new Array();

markers[0] = new google.maps.Marker({
    position: new google.maps.LatLng(45.910078, 10.838013),
    map: map, 
    title: "Data 1"
});

google.maps.event.addListener(markers[0], 'click', function() {
    var infowindow = new google.maps.InfoWindow({
        content: $('#map_info_window_id').html()
    });

    infowindow.open(map, markers[0]);
});

markers[1] = new google.maps.Marker({
    position: new google.maps.LatLng(46.176086, 11.064048),
    map: map,
    title: "Data 2"
});

google.maps.event.addListener(markers[1], 'click', function() {
    var infowindow = new google.maps.InfoWindow({
        content: $('#map_info_window_id').html()
    });

    infowindow.open(map, markers[1]); 
});

以及如何看,我有一个每个标记的监听器!

现在,当我点击一个标记时,我想关闭所有标记(实际上只有一个,先前打开过)。

如何在Google Maps API 3上执行此操作?

1 个答案:

答案 0 :(得分:1)

将标记的地图设置为null:

marker.setMap(null);
相关问题