How to remove old marker and Then setInterval new marker...I'm calling an API and plotting new markers after few seconds setinterval. The new markers get plotted but the old one still remains. I want to clear the old marker when the new markers are plotted. Here is my code.
window.setInterval(selectLocation, 1000);
var map,mIcon;
function initMap() {
var myOptions = {
zoom: 17,
center: new google.maps.LatLng(18.803535629803218, 98.95209518731684),
mapTypeId:google.maps.MapTypeId.ROADMAP
};
mIcon = {
path: google.maps.SymbolPath.CIRCLE,
fillColor: "green",
fillOpacity: 0.8,
strokeOpacity: 0.5,
strokeWeight: 6,
strokeColor: '#1b7709',
scale: 30
};
map = new google.maps.Map(document.getElementById('map_canvas'),
myOptions);
infowindow = new google.maps.InfoWindow({
map:map,
});
}
let json;
let marker = [];
function selectLocation(){
$.ajax({
type:"POST",
url: "json_location.php",
}).done(function(text){
json = $.parseJSON(text);
for(var i = 0 ;i<json.length;i++){
var lat = json[i].lat;
var lng = json[i].lng;
var id_local = json[i].id_local;
var name_local = json[i].name_local;
var latlng = new google.maps.LatLng(lat,lng);
var id = i;
marker = new google.maps.Marker({
map:map,
html:id,
position:latlng,
icon: mIcon,
label: {color: '#FFF', fontSize: '14px', fontWeight: '600', text: json[i].id_local},
title: json[i].id_local,
})
})
}