I'm writing an app that loads Google Maps asynchronously with a hand-built framework.
When I load maps it will not load all of it for some reason and I'll end up with a Uncaught TypeError: undefined is not a function
. I checked chrome inspector and found out that google.maps
is a valid object, but it has none of its own properties. I manually call the "initialize function" well after the document has loaded. What am I doing wrong?!

- 7,994
- 9
- 44
- 73
-
1Perhaps some code (your "hand-built framework would be a start)? A link to a page that exhibits the problem? A jsfiddle that does? – geocodezip Jan 06 '13 at 18:00
-
1Reading the documentation is a good start [**See**](https://developers.google.com/maps/documentation/javascript/tutorial#asynch) – david strachan Jan 06 '13 at 20:17
1 Answers
You can't load the maps-API asynchronous with the well-known URL( http://maps.googleapis.com/maps/api/js?v=3&sensor=false )
When you take a look at the script-file, you'll see, that this is not the API that gets loaded, it's a loader that loads the API. The loader makes use of document.write()
, what will lead you to unexpected results when called after the document has been loaded.
Furthermore the onload-event of the document doesn't wait for asynchronous loaded objects, it may come too quick.
You also cannot use the load-event of the script to invoke the initialize-function, because when it fires, the loader is loaded, not the maps-API.
What to do:
append a callback-parameter to the script-URL(with the name of the initialize-function as value)
http://maps.googleapis.com/maps/api/js?v=3&sensor=false&callback=initialize
Now you get a different loader which:
- doesn't use
document.write()
- calls the callback-function(initialize) when the maps-API has been loaded
Demo: http://jsfiddle.net/doktormolle/7cu2F/
Related to the comment: seems the callback has to be a function attached to window directly. not cool google :)
There is another option, the google-API-loader which supports the usage of function-references (instead of function-names).
Sample, which loads the maps-API asynchronously, but only when there is an element with the ID map-canvas
in the document, and then creates a map:
window.addEventListener('load',function(){
if(document.getElementById('map-canvas')){
google.load("maps", "3",{
callback:function(){
new google.maps.Map(document.getElementById('map-canvas'), {
center: new google.maps.LatLng(0,0),
zoom: 3
});
}
});
}
},false);
body,html,#map-canvas{
height:100%;
margin:0;
padding:0;
width:100%;
}
<script src="https://www.google.com/jsapi?.js"></script>
<div id="map-canvas"></div>

- 116,463
- 16
- 195
- 201
-
Ok thanks for the info. I didn't want to use the `callback=initialize` because I wanted to pass some extra arguments in the initialize function, but I guess I'll have to work around it. – Stephen Jan 07 '13 at 21:11
-
seems the `callback` has to be a function attached to window directly. not cool google :) – apneadiving Feb 13 '15 at 16:57
-
1@apneadiving: The google-API-loader would be an option in this case, see my edited answer. – Dr.Molle Feb 13 '15 at 23:01
-
1@Dr.Molle 1 in 5 page loads still gives the error with that script. Is caching involved on Google's end or something? – Ben Racicot May 16 '15 at 21:15
-
2There is another great solution that worked for me. It is about using Google API Loader (explained [here](http://blog.rtwilson.com/how-to-load-the-google-maps-places-library-through-google-api-loader/comment-page-1/#comment-175489)). – Andrew F. Mar 29 '16 at 12:22
-