I think the real question here is "On your page, what does it mean for a map to be initialized or not?"
In a comment you mentioned that you may have about 200 maps on the page. Surely you don't mean that you've called new google.maps.Map()
for each of those 200 maps? That would seem to make for a very slow loading page.
Is it actually that you have 200 container <div>
elements, some of which may have been initialized with a map using new google.maps.Map()
as in your initialize()
function, and some of which have not yet been initialized with that code but are simply empty <div>
elements until you do that?
And could it be that what you're really interested in is not so much whether a particular map has fully loaded, but simply whether you've started loading it?
As an example, you could have a page where you click on a box and it starts loading a map. Once the map has started loading, you don't want to reload it on a second click. What you're interested in this case is not whether the map has fully loaded, but whether it has started loading at all.
If that's what you're doing, then one simple way to test it is to see if the <div>
element has any child elements or not:
function hasMap( id ) {
return !! document.getElementById(id).firstChild;
}
if( hasMap("map-canvas-0") ) {
// That map has loaded or at least has started loading
}
If you need to find out if a map is fully loaded, then the idle
event is the way to do it. At that point it's really a tossup whether to use a hidden field or a JavaScript object. A JavaScript object is really simple:
var mapsLoaded = {};
function loadMap( id, lat, lng, zoom ) {
var options = {
center: new google.maps.LatLng( lat, lng ),
zoom: zoom,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var container = document.getElementById( id );
var map = new google.maps.Map( container, options );
google.maps.event.addListener( map, 'idle' function() {
mapsLoaded[id] = true;
});
}
Now, if you want to know if a map of a particular ID is loaded, simply use:
mapsLoaded[id]
e.g.
mapsLoaded['map-canvas-0']
will tell you if the map from your example is fully loaded.
If this answer seems way off base, how about explaining more fully what you're doing? When do you create your container elements—all when the page is initialized, or later? When and how does a map get created? On a user click or what? How do the container IDs get generated? What is the situation where you need to know whether a map has been created—when do you need to know this and why?
The more you can clearly specify these kinds of questions, the more likely it will be that someone can help you with a specific answer—and the more likely that you'll have an "aha" moment and figure out exactly what to do. :-)