在我的第一次点击活动中,所有标记都显示在谷歌地图中,我想在第二次点击中删除它们。当我执行我的代码时,只删除最后一个标记。
这是我的javascript代码:
var showmarkers = false;
google.maps.event.addDomListener(hotel, 'click', function () {
if (showmarkers == false) {
showmarkers = true;
for (var i = 0; i < len; i++) {
//add makers
marker = new google.maps.Marker({
position: new google.maps.LatLng(results.rows.item(i).lat, results.rows.item(i).long),
map: map,
icon: icons[1],
animation: google.maps.Animation.DROP,
});
markers.push(marker);
}
//add infowindow
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
//if we create the infowindow here, all the windows 'll stay shown
infowindow.setContent("<div style='background-color:red;'><h3>" + results.rows.item(i).nom + "</h3><br/><center>" + "<img src='" + results.rows.item(i).img + "' style='width:20px; height:20px;' /></center><br/></div>")
infowindow.open(map, marker);
}
})
(marker, i));
} else {
alert("false");
showmarkers = true;
marker.setMap(null);
}
答案 0 :(得分:1)
else {
alert("false");
showmarkers = true;
for(var lp=0;lp<markers.length;lp++){
markers[lp].setMap(null);
}
}