I am currently having problems clearing the past google maps marker i have a clearoverlays function but it does not work here is the code
var markers=[];
var customLabel = {
restaurant: {
label: 'R'
},
bar: {
label: 'B'
}
};
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(17.1899, -88.534721),
zoom: 8
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP or XML file
setInterval(function() {downloadUrl('xml1.php', function(data) {
var xml = data.responseXML;
markers = xml.documentElement.getElementsByTagName('marker');
Array.prototype.forEach.call(markers, function(markerElem) {
var id = markerElem.getAttribute('id');
var name = markerElem.getAttribute('name');
var address = markerElem.getAttribute('address');
var type = markerElem.getAttribute('type');
var point = new google.maps.LatLng(
parseFloat(markerElem.getAttribute('lat')),
parseFloat(markerElem.getAttribute('lng')));
var infowincontent = document.createElement('div');
var strong = document.createElement('strong');
strong.textContent = name
infowincontent.appendChild(strong);
infowincontent.appendChild(document.createElement('br'));
var text = document.createElement('text');
text.textContent = id
infowincontent.appendChild(text);
var icon = customLabel[id] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
label: icon.label
});
marker.addListener('click', function() {
infoWindow.setContent(infowincontent);
infoWindow.open(map, marker);
});
});
clearOverlays();
});
}, 0500);
}
function clearOverlays() {
for (var i = 0; i < markers.length; i++ ) {
markers[i].setMap(null);
}
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
the set interval function just downloads the information from multiple people automatically from mysql database every half a second to plug in to the map it works great only that it leaves previous locations as markers on the map.
What I have tried:
ive tried making the clearoverlay run in a set interval as well but it does not work also i have tried putting the clearoverlay right after this
setInterval(function() {downloadUrl('xml1.php', function(data) {
but if i do this it does not update the locations until i manually refresh the page