本文介绍了删除所有标记谷歌地图v3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的第一次点击事件中,所有标记都显示在谷歌地图中,我想在第二次点击中将其删除。当我执行我的代码时,只有最后一个标记被删除。
这是我的javascript代码:
var showmarkers = false;
google.maps.event.addDomListener(hotel,'click',function(){
$ b $ if(showmarkers == false){
showmarkers = true;
for(var i = 0; i< len; i ++){
// add maker
marker = new google.maps.Marker({
position:new google。 maps.LatLng(results.rows.item(i).lat,results.rows.item(i).long),
map:map,
icon:图标[1],
动画:google.maps.Animation.DROP,
));
markers.push(marker);
}
/ / add infowindow
google.maps.event.addListener(marker,'click',(function(marker,i)){
return function(){
//如果我们在这里创建infowindow ,所有的窗口都会显示
infowindow.setContent(< div style =背景颜色:红色;>< h3>+ results.rows.item(i).nom +< / h3>< br>< center>+< img src = '+ results.rows.item(i).img +'style ='width:20px; < / center>< br />< / div>)
infowindow.open(map,marker);
}
})
(marker,i));
} else {
alert(false);
showmarkers = true;
marker.setMap(null );
}
解决方案
else {
alert(false);
showmarkers = true;
for(var lp = 0; lp< markers.length; lp ++){
marker [lp] .setMap(null);
}
}
In my first click event all markers are shown in google map, I want to remove them in the second click. when I execute my code, only the last marker is removed.
this is my javascript code:
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);
}
解决方案
else {
alert("false");
showmarkers = true;
for(var lp=0;lp<markers.length;lp++){
markers[lp].setMap(null);
}
}
这篇关于删除所有标记谷歌地图v3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
08-27 13:57