问题
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 but 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" );
}
回答1:
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.
来源:https://stackoverflow.com/questions/3682017/google-maps-api-v2-load-event-not-firing