How to check if Google Maps API (v3) is loaded?
I do not want to execute mapping scripts if the API did not load due to internet connectivity problems (web page is h
None of the current answers are working with 100% consistency for me (excluding Google Loader, which I haven't tried). I don't think checking the existence of google.maps is enough to be sure the library has finished loading. Here are the network requests I'm seeing when the Maps API and optional Places library are requested:
That very first script defines google.maps, but the code that you probably need (google.maps.Map, google.maps.places) won't be around until some of the other scripts have loaded.
It is much safer to define a callback when loading the Maps API. @verti's answer is almost correct, but still relies on checking google.maps unsafely.
Instead, do this:
HTML:
JS:
var isMapsApiLoaded = false;
window.mapsCallback = function () {
isMapsApiLoaded = true;
// initialize map, etc.
};