Skip to content

Instantly share code, notes, and snippets.

@tormjens
Last active December 9, 2016 09:43
Show Gist options
  • Save tormjens/42b353293fe956fe17bc3fe28a7a6895 to your computer and use it in GitHub Desktop.
Save tormjens/42b353293fe956fe17bc3fe28a7a6895 to your computer and use it in GitHub Desktop.
Simple Google Maps Async Loader Using Deferred Object (ES6/jQuery)
export default function(ApiKey, libraries) {
var promise = jQuery.Deferred();
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://maps.google.com/maps/api/js?key='+ApiKey;
if(libraries)
script.src += '&libraries='+ libraries
document.body.appendChild(script);
jQuery('body').addClass('google-maps-ready');
var interval = setInterval(() => {
if(typeof window.google !== 'undefined' && typeof google.maps !== 'undefined') {
promise.resolve();
clearInterval(interval);
}
}, 200)// succeed
return promise.promise();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment