clear Overlayers

2,419 views
Skip to first unread message

christoph beckmann

unread,
Jul 20, 2009, 4:52:25 PM7/20/09
to Google Maps JavaScript API v3
Hello,

in the map api 2.x give it the function "clearOverlays" to delete all
markers on a map.
Is there a similar function in 3.x?
If not, what opportunities there are to delete any marker on the map?

http://code.google.com/intl/de-DE/apis/maps/documentation/reference.html#GMap2.clearOverlays

Thanks for your help.
I'm looking forward to hearing from you, best regards
Christoph Beckmann

Esa

unread,
Jul 20, 2009, 6:47:58 PM7/20/09
to Google Maps JavaScript API v3
Every time you create a marker, push it in an array. Then you can
clear the map and the array by:

function clearMarkers(){
for(var i=0; i<markers.length; i++){
markers[i].set_map(null);
}
markers.length = 0;
};

Hank

unread,
Jul 31, 2009, 10:55:40 AM7/31/09
to Google Maps JavaScript API v3
I too have been unable to find good documentation of how to remove
markers.

I tried the function Esa provided but it does NOTHING.

http://d9qjx79zqpo3.googlepages.com/clearMarkers.html

How can I remove all markers in my example, posted about?

Christoph Beckmann

unread,
Jul 31, 2009, 10:57:36 AM7/31/09
to google-map...@googlegroups.com

Esa

unread,
Jul 31, 2009, 11:36:59 AM7/31/09
to Google Maps JavaScript API v3


On Jul 31, 5:55 pm, Hank <hankkni...@gmail.com> wrote:

> I tried the function Esa provided but it does NOTHING.
>

You should declare an array

var markers = [];

and every time you create a marker, push it in the array.

function placeMarker(location) {
marker = new google.maps.Marker({
position: location,
map: map
markers.push(marker);
});

Now you can use the clearMarkers() function provided.

Dan H

unread,
Aug 15, 2009, 6:50:45 PM8/15/09
to Google Maps JavaScript API v3
Esa's method worked for me. Overlooking the array stuff, the answer
to

"How do you clear a marker from a map?"

the answer seems to be:

"Use the set_map method of the marker, with the argument 'null'."

In my case, I only want to keep ONE marker on the map, so I can keep
track of the previous marker in a single variable.

This is my code snippet:

var prevMarker; // put this somewhere with global scope, so it
persists

// elsewhere, when you want to put a new marker at "latlng",
and get rid of the old marker...

var marker = new google.maps.Marker({
position: latlng,
map: map,
title:"Hello World!"
});

if (prevMarker) {
prevMarker.set_map(null);
};
prevMarker = marker;

Now, does this mean that marker objects are piling up somewhere? It
feels like they might, as I didn't actually "delete" the previous
marker object. I guess you have to trust JavaScript to garbage
collect if/when there are no more references.


Stephen J. Fuhry

unread,
Aug 16, 2009, 9:34:40 AM8/16/09
to Google Maps JavaScript API v3
They should not be piling up, as you are effectively clearing any
previous value of prevMarker when you do `prevMarker = marker` and
`marker = new google.maps.Marker(...`

If you ever want to do anything more advanced such as clear the
markers that are not on the map as the map moves, you will have to use
hash tables. This is when you have to start worrying about deleting
values to keep them from piling up.

This code has been tested, and uses jQuery:

// this creates a marker and stores it in a hash table, allMarkers, so
i can perform actions on markers later without difficulty.
function createMarker(lat,lng,name,id) {
allMarkers[id] = {map:new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
map: map,
title: name,
clickable: true
})};
google.maps.event.addListener(allMarkers[id].map, 'click', function()
{
$.ajax({
type : "POST",
url : "data/getInfoWindow",
data : {id:id},
beforeSend: function() {
infowindow.set_content("loading...");
infowindow.open(map,allMarkers[id].map);
},
success : function(data){
infowindow.set_content(data); }
});
});
}


// this clears all markers on the map
function clearMarkers() {
$("div#numChurches").html("");
for (var i in allMarkers) {
allMarkers[i].map.set_map(null);
}
delete allMarkers;
}

// this deletes any markers not currently on the map
var bounds = map.get_bounds();
for (var i in allMarkers) {
if (!bounds.contains(allMarkers[i].map.get_position())) {
// must clear AND delete allMarkers[i], so the markers don't begin
to pile up
allMarkers[i].map.set_map(null);
delete allMarkers[i];
}
numMarkers++;
}

Reply all
Reply to author
Forward
0 new messages