Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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.4k 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

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.6k users

Browse Categories

...