I’m attempting to load google maps in an anonymous function but am getting a javascript error whenever I try to use one of the api’s methods. For example:
Code
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
Error
TypeError: google.maps.LatLng is not a constructor
I’ve created 2 examples:
1) This loads google map js via script tag in the body of the page. This method DOES work and there is no js error. http://jsfiddle.net/malonso/hgPQk/1/
2) This loads google map js w/in the anonymous function. This method does NOT work and contains the js error mentioned above. http://jsfiddle.net/malonso/fZqqW/2/
I am sure I’m missing something glaringly obvious but I simply cannot figure out what. Thanks in advance.
Update: I should point out that it is a requirement that google maps be loaded from w/in the anonymous function.
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