谷歌地图和阿贾克斯我可以添加标记,但我无法删除或隐藏它们
google maps and ajax i can add the markes but i am not able to delete or hide them
function checkPostServer(){
$.ajax({
type : "POST",
url : "/getIngo/",
data : {
msg: chkToServer
},
dataType: 'json',
async: false,
success: function(data) {
disMarker(data)
}
});
}
function disMarker(data){
if (data) {
$.each(data, function (i, item) {
createMarker(item);
});
}
}
function createMarker(data){
var latLng = new google.maps.LatLng(data.lat, data.lng);
// ...and add the Marker to your map
var marker = new google.maps.Marker({
position: latLng,
map: map
});
//console.log(data);
markers2[data.key] = marker;
marker.setMap(map);
}
}, 2000);
setInterval(function() {
markers2[2].setMap(null);
alert("Here");
}, 4000);
你应该做这样的事情
var markers = []; // this variable must be within a scope where the functions can read it (for example global scope)
// ...
function disMarker(data) {
if (data) {
// first clear all markers
clearMarkers();
$.each(data, function (i, item) {
createMarker(item);
});
}
}
function createMarker(data) {
var latLng = new google.maps.LatLng(data.lat, data.lng);
var marker = new google.maps.Marker({
position: latLng,
map: map // this line replaces marker.setMap(map); you don't need both
});
// now we add this object to the markers array
markers.push(marker);
}
function clearMarkers() {
for (var i=0; i<markers.length; i++) {
markers[i].setMap(null); // this removes the marker from the map
}
markers = []; // now this variable is an empty array again; and we can fill it with new marker-orbects
}
我不知道标记2是关于什么的;我不使用它
相关文章:
- 添加和删除隐藏字段数组中的值,而不提交表单
- 循环后如何删除/隐藏图像
- 删除/隐藏 javascript 中的选择选项而不会使其看起来很尴尬(需要延迟 select 元素的本机 onclick
- 如何从内部HTML字符串中删除隐藏的标签
- 从 Blogger 中的 post.body 中删除/隐藏元素
- 删除/隐藏链接后最后一次出现的字符串
- 按隐藏字段的值删除隐藏字段
- 从克隆的元素中删除隐藏的元素
- 如何使用 jquery 在滚动到底部时正确从元素中删除隐藏类
- 计算 jQuery text().Length of Visible 元素而不删除隐藏元素
- 删除/隐藏表<tr>(s) 其<td>(s) 没有文本
- 如何删除隐藏后的李空间
- 如何从选择列表中删除/隐藏选择选项
- 删除/隐藏TinyMCE而不删除格式
- Javascript-在表单提交之前删除隐藏字段或设置为空
- jQuery删除隐藏了匹配文件名的行
- 如何删除隐藏的HTML元素
- 当复选框未选中时,删除隐藏字段中的值
- 使用Javascript删除/隐藏具有特定类的span标签
- 删除隐藏字段后,Django出现CSRF错误