title: How to remove markers using marker.setMap(null) from google maps API - JavaScript
body: <p>The idea is to make dynamic markers using google maps API.
  I want to remove the created markers first before new markers show up. I don't want to make any Arry to manage markers. because I simply want them to be removed without putting them to an array and then delete it from there.</p>
  
  <p>I am calling <code>deleteMarker</code> function inside the loop to do this.</p>
  
  <p>I have used a global variable but 
  I get this error:</p>
  
  <pre><code>TypeError: Mark is undefined[Learn More]
  </code></pre>
  
  <p>code: </p>
  
  <pre><code>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;
  }
  </code></pre>