Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (20.3k points)

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?

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

1 Answer

0 votes
by (40.7k points)

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.length; i++ ) {

    markersArray[i].setMap(null);

  }

  markersArray.length = 0;

}

Otherwise, you can use this:

google.maps.Map.prototype.clearOverlays = function() {

  for (var i = 0; i < markersArray.length; i++ ) {

    markersArray[i].setMap(null);

  }

  markersArray.length = 0;

}

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.

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...