Im trying to load the google maps api's dynamically. I'm using the following code:
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.src= 'http://www.google.com/jsapi?key=<MY_KEY>;
head.appendChild(script);
but when trying to create the map
map = new GMap2(document.getElementById("map"));
or
map = new google.maps.Map2(document.getElementById("map"));
I'm getting an error that google (or GMap2) is undefined.
Make sure that you aren't instantiating the map before the Javascript has finished loading.
Also, If you want to use the AJAX API loader, you need to do it like this:
Otherwise, just use the regular Maps API script source:
You can do this. You can add a callback function name in the url. It'll be called when the API gets loaded. That callback function must be accessible in the document's scope.
I did that some time ago by triggering a custom event on the window with jQuery: http://jsfiddle.net/fZqqW/5/
used "http://maps.google.com/maps/api/js?sensor=false&callback=gMapsCallback"
See HERE : https://developers.google.com/maps/documentation/javascript/tutorial
I've done it like so... this example uses jQuery and google map v3.x
This works for me: