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?!
There are a several reasons why your google maps may not be working, the most common issue being no Google Map API key set or set incorrectly. To use the Google Maps JavaScript API, you must register your app project on the Google Cloud Platform Console and get a Google API key which you can add to your app.
Note: Starting in 2020, new APIs only support Promises. See this guide on using Promises or the examples below for making asynchronous method calls with Google Maps JavaScript API. The await operator is used to wait for a Promise. It can only be used inside an async function.
The Maps JavaScript API returns both errors and warnings. An error indicates a severe issue which occurred while loading the Maps JavaScript API. Your page cannot load the API correctly, and the API will not work on that page. A warning is a supplemental message about the loading of the Maps JavaScript API.
Asynchronous methods throughout Google Maps JavaScript API will soon be returning Promises . Currently, these methods take a request object and a callback. In the future, this callback will become optional and a Promise will always be returned. Promise support requires the use of the beta version of Maps JS.
Use the Google Maps Platform API Checker Chrome extension. This allows you to determine if your website is properly implementing Google’s licensed Maps APIs. If you are using a library or plugin to load the Maps JavaScript API, check the settings for that library and look for an API key option.
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:
document.write()
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With