Skip to content

Instantly share code, notes, and snippets.

@jorgepedret
Created April 20, 2012 23:01
Show Gist options
  • Select an option

  • Save jorgepedret/2432506 to your computer and use it in GitHub Desktop.

Select an option

Save jorgepedret/2432506 to your computer and use it in GitHub Desktop.
Loading Google Maps API on demand
// main.js
function loadGoogleMapsApi(){
if(typeof google === "undefined"){
var script = document.createElement("script");
script.src = "https://maps.google.com/maps/api/js?sensor=false&callback=loadGoogleMapsApiReady";
document.getElementsByTagName("head")[0].appendChild(script);
} else {
loadGoogleMapsApiReady();
}
}
function loadGoogleMapsApiReady(){
$("body").trigger("gmap_loaded");
}
// other.js
$("body").bind("gmap_loaded", function(){
alert("Google map is loaded and ready to be used!");
});
loadGoogleMapsApi();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment