1
votes

I use Google Maps API V3.

I migrate My site GMap API V2 -> V3. but coped because I used the clearOverlays() method to delete an existing object at once in the V2 , and it deleted an object in what had been abolished in V3 individually.

V2

map.clearOverlays();

V3

map.set_visible(false);
popup.close();

is it OK? or other solutions?

4
I wrote a quick blog about it and put some sample code up as well joelee.me.uk/2010/07/27/clearoverlays-in-google-maps-v3-apithiswayup
Just look at this example that is in the Google docs: code.google.com/apis/maps/documentation/javascript/examples/…Alby Barber

4 Answers

7
votes

I couldn't find anything in the V3 reference which will explicitly clear the overlays created on the map and I could't find any code samples which do this either.

I think that it is important to point out the V3 API is a very early developer release and contains only a very basic set of features. It is quite possible that they simply haven't got to this functionality yet. If you are working on a live system, I suggest you stick with V2 until V3 is more mature.

There is a post on the developer group which asks the question about how the API should be used with a very useful answer from the V3 product manager:

Misconception about v3 Options

EDIT:

OK, it seems like this functionality is missing from the API deliberately in order to keep it lightweight. You should keep track of your overlay objects yourself and call:

object.set_map(null);

to remove them.

5
votes

Here is what I do:

create an empty array literal, push the markers as you make them, and then evict them when necessary

var eviction_list = [];

function evictMarkers() {

    // clear all markers

    $(eviction_list).each(function () {
         this.set_map(null);
    });

    // reset the eviction array 
    eviction_list = [];
}

//in function adding markers
......
marker = new google.maps.Marker({
    position: results[0].geometry.location,
    map: map,
    title: elem.title
});
eviction_list.push(marker);
......

//to clear all markers
evictMarkers();
3
votes

As hongwei correctly mentions, the function is called setMap(), not set_map().

See http://code.google.com/apis/maps/documentation/v3/overlays.html#HideShow

1
votes

The problem is that setMap(null) can be slow when you have many markers.