Google Maps API v3 - How to clear overlays?
Solution 1
See here for details on the various options open to you but you now have to iterate through the markers and remove them individually. Your code should look something like this:
var markers = [];
function clearOverlays() {
while(markers.length) { markers.pop().setMap(null); }
markers.length = 0;
}
markers.push(marker);
google.maps.event.addListener(marker,"click",function(){});
Solution 2
This is good one:
http://apitricks.blogspot.com/2010/02/clearoverlays-in-v3.html
Article in case the link dies:
clearOverlays() in V3
There is no clearOverlays() in API v3. Some practices have been presented. I think this is the simpliest so far.
Push all the overlays in an array when created (as usual). Following code will clear both map and the array:
while(overlays[0])
{
overlays.pop().setMap(null);
}
pop() method of an array removes the last element of an array, and returns that element. 'while' keeps that happening as long as there are elements in the array. When overlays[0] does not exist anymore, the mission is completed and code will proceed.
Solution 3
I found another solution and it works very good it will remove all overlays that exist on the map
gmap.overlayMapTypes.setAt( 0, null);
while gmap is your map object
Solution 4
The overlayMapTypes
object brings a clear method:
map.overlayMapTypes.clear()
Whereas map is your Google Maps object.
If you cannot find the method in your API version, you can resort to the following source of clear
:
clear = function() {
for (; this.get("length");) this.pop()
};
Solution 5
You can take a look at the Google Maps documentation as it show simple deleteOverLays method http://code.google.com/apis/maps/documentation/javascript/overlays.html
// Deletes all markers in the array by removing references to them
function deleteOverlays() {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
}
Related videos on Youtube
Natim
Work for Ionyse.com My CV here : http://remy.hubscher.crealio.fr/
Updated on July 09, 2022Comments
-
Natim almost 2 years
In Google Maps API v2, I was using
map.clearOverlays()
to remove the marker and draw them again.How can I do that using Google Maps API v3 ?
Thanks
-
Praveen almost 10 yearspossible duplicate of Google Maps API v3: How to remove all markers?
-
Natim almost 10 yearsIndeed since it is the post linked to the accepted answer.
-
-
tony gil about 12 yearsremoving from map is HIDING, not DELETING. the markers still exist, but you got rid of the array that held them. you must setMap(null) then set the marker itself to null. code.google.com/apis/maps/documentation/javascript/…
-
tony gil about 12 yearsVERY GOOD SOLUTION! i had problems implementing your function until i saved the pop()èd polyline into a temp object first: var arrayTemp = overlayArrayPolylineUpload.pop(); arrayTemp.setMap(null);
-
josliber over 8 yearsThis answer has been flagged for removal because it is a link-only answer. Could you please expand this answer so it provides an answer to the question without requiring the reader to click to the linked webpage?