Google Maps API v3: How to remove all markers?

1.1K    Asked by IraJoshi in Devops , Asked on Jul 12, 2021

 In Google Maps API v2, if I wanted to remove all the map markers, I could simply do:

map.clearOverlays();

How do I do this in Google Maps API v3? And how google maps clear markers?

Looking at the Reference API, it's unclear to me.

Answered by Behailu

To map clear markers, you can try using the following:

1. Declare a global variable like this:

var markersArray = [];
2. Define a function like this:
function clearOverlays() {
  for (var i = 0; i < markersArray xss=removed xss=removed xss=removed xss=removed>

3. Push markers in the 'markerArray' before calling the following:

markersArray.push(marker);
google.maps.event.addListener(marker,"click",function(){});

4. Call the clearOverlays(); or map.clearOverlays(); function wherever required.



Your Answer