这个想法是使用Google Maps API制作动态标记。我想在出现新标记之前先删除创建的标记。我不想做任何Arry来管理标记。因为我只是希望将它们删除而无需将它们放到数组中,然后将其从那里删除。
我在循环中调用 deleteMarker
函数以执行此操作。
我使用了全局变量,但是我得到这个错误:
TypeError: Mark is undefined[Learn More]
代码:
var map;
var Mark;
//setInterval(function(){ alert("Hello"); }, 3000);
setInterval(ajaxCall, 9000); //300000 MS == 5 minutes
function ajaxCall() {
$.ajax({
url: '/maps/my_ajax_request/',
datatype: 'json',
type: 'GET',
success: function (data) {
for(var prop in data) {
var item = data[prop];
console.log(item.latitude,item.longitude, item.icon);
if (item.icon === "online") {
selected_status = online;
} else {
selected_status = offline;
}
deleteMarker();
this.Mark = new google.maps.Marker({
position: {lat: (parseFloat(item.latitude)), lng: (parseFloat(item.longitude))},
map: map,
icon: selected_status
});
}
},
failure: function(data1) {
alert("Got an error dude");
}
});
}
function deleteMarker() {
Mark.setMap(null);
Mark=null;
}
var map;
var Mark;
var online = 'https://img.icons8.com/ultraviolet/48/000000/marker.png';
var offline = 'https://img.icons8.com/color/48/000000/marker.png';
setInterval(ajaxCall, 9000); // 300000ms == 5 minutes
function ajaxCall() {
$.ajax({
url: '/maps/my_ajax_request/',
datatype: 'json',
type: 'GET',
success: function (data) {
for(var prop in data) {
var item = data[prop];
console.log(item.latitude,item.longitude, item.icon);
if (item.icon === "online") {
selected_status = online;
} else {
selected_status = offline;
}
deleteMarker();
Mark = new google.maps.Marker({ // no need of use `this` keyword when use as variable
position: {lat: (parseFloat(item.latitude)), lng: (parseFloat(item.longitude))},
map: map,
icon: selected_status
});
}
},
failure: function(data1) {
alert("Got an error dude");
}
});
}
function deleteMarker() {
if (Mark) {
Mark.setMap(null);
Mark = null;
}
}