Google Maps API v2 load() event not firing
I'm improving the Google Maps API V2 integration in our web app and I'd like my main page to know when Google Maps has finished loading everything so I can then set some markers.
I notice there's a load() event bu开发者_JS百科t I can never seem to get it fire.
Here's the code I'm using
if( GBrowserIsCompatible() ) {
map = new GMap2(container);
map.setCenter(new GLatLng(INITIAL_LATITUDE,INITIAL_LONGITUDE), INITIAL_ZOOM);
GEvent.addListener(map, "load", pluginLoaded );
}
...
function pluginLoaded() {
alert( "pluginLoaded" );
}
The load
event is not firing because it gets triggered soon after you call setCenter()
, and at that time your event listener is not attached yet. You can see the event being triggered in the following example:
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
GEvent.addListener(map, "load", function() {
alert("Map Loaded");
});
map.setCenter(new GLatLng(37.4419, -122.1419), 13);
}
Note that there is no need to listen for the load
event to start adding markers to the map.
精彩评论