views:

18792

answers:

5

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.

+5  A: 

It seems that there is no such function in V3 yet.

People suggest to keep references to all markers you have on the map in an array. And then when you want to delete em all, just loop trough the array and call .setMap(null) method on each of the references.

See this question for more info/code.

My version:

google.maps.Map.prototype.markers = new Array();

google.maps.Map.prototype.getMarkers = function() {
    return this.markers
};

google.maps.Map.prototype.clearMarkers = function() {
    for(var i=0; i<this.markers.length; i++){
        this.markers[i].setMap(null);
    }
    this.markers = new Array();
};

google.maps.Marker.prototype._setMap = google.maps.Marker.prototype.setMap;

google.maps.Marker.prototype.setMap = function(map) {
    if (map) {
        map.markers[map.markers.length] = this;
    }
    this._setMap(map);
}

The code is edited version of this code http://www.lootogo.com/googlemapsapi3/markerPlugin.html I removed the need to call addMarker manually.

Pros

  • Doing this way you keep the code compact and in one place (doesn't pollute the namespace).
  • You don't have to keep track of the markers yourself anymore you can always find all the markers on the map by calling map.getMarkers()

Cons

  • Using prototypes and wrappers like I did now makes my code dependent on Google code, if they make a mayor change in their source this will break.
  • If you don't understand it then you won't be able to fix it if does break. The chances are low that they're going to change anything which will break this, but still..
  • If you remove one marker manually, it's reference will still be in markers array. (You could edit my setMap method to fix it, but at the cost of looping trough markers array and removing the reference)
Maiku Mori
+1 From me. But your answer would be better if you included the wrapper to call addMarker automatically!
Andrew
I assume you're referring to Andrews answer actually. Would you show with code what you'd do different and why. Thanks
mp_
Coming right up in a sec =)
Maiku Mori
Thanks Maiku, looking forward to seeing your awesome :)
mp_
Meh sorry for delay, I was holding back from posting code because I had no way to quickly test it.
Maiku Mori
Thanks Maiku. Though, I don't understand - how do I add a new marker in your example. Again, many many thanks!
mp_
+5  A: 
google.maps.Map.prototype.markers = new Array();

google.maps.Map.prototype.addMarker = function(marker) {
    this.markers[this.markers.length] = marker;
};

google.maps.Map.prototype.getMarkers = function() {
    return this.markers
};

google.maps.Map.prototype.clearMarkers = function() {
    for(var i=0; i<this.markers.length; i++){
        this.markers[i].setMap(null);
    }
    this.markers = new Array();
};

I don't think there is one in V3 so I used the above custom implementation.

Disclaimer: I did not write this code but I forgot to retain a reference when I merged it into my codebase so I don't know where it came from.

Andrew
+1 From me. I'd add a wrapper around google.maps.Marker constructor (or setMap method since I think the constructor calls it internally) which calls addMarker automatically, but still nice answer :).
Maiku Mori
@Maiku Mari, would you show with code what you'd do different and why. Thanks
mp_
Frederico
I believe it came from here http://www.lootogo.com/googlemapsapi3/markerPlugin.html
Maiku Mori
-1 Bad style. There is only one markers array created, but one per map after clearMarkers (cause of get/set difference with prototypes). Nasty bugs with multiple map objects.
Tomas
+2  A: 

The "set_map" function posted in both answers appears to no longer work in Google Maps v3 API.

I wonder what happened

Update:

It appears Google changed their API such that "set_map" is not "setMap".

http://code.google.com/apis/maps/documentation/v3/reference.html

GregN
+8  A: 

Same problem. This code doesn't work anymore.

I've corrected it, change clearMarkers method this way:

set_map(null) ---> setMap(null)

google.maps.Map.prototype.clearMarkers = function() {
    for(var i=0; i < this.markers.length; i++){
        this.markers[i].setMap(null);
    }
    this.markers = new Array();
};
Pons
Thanks! And thank you Guido for updating our posts.
Maiku Mori
The way I finally got it working was to iterate through the markers collection where I stored them and use setMap(null)
Sebastian
But does this clear the markers from memory? I realize JavaScript has automatic garbage collection, but how do we know Google's API does not hold a reference to the marker when setMap(null) is called? In my application, I add and "delete" a ton of markers, and I would hate for all those "deleted" markers to be sucking up memory.
Nick
+7  A: 

Simply do the following:

I. Declare a global variable:

var markersArray = [];

II. Define a function:

function clearOverlays() {
  if (markersArray) {
    for (i in markersArray) {
      markersArray[i].setMap(null);
    }
  }
}

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

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

IV. Call the clearOverlays(); function wherever required.

That's it!!

Hope that will help you.