Loading google maps asynchronously

The map appears twice because you’re calling initialize twice.

Before fixing that, let’s simplify your code a bit. Never let yourself repeat blocks of code like that; instead make it into a common function.

Also, don’t load infobox.js from googlecode.com; Google Code is not a CDN. Load your own copy.

So, the code may look like this:

function addScript( url, callback ) {
    var script = document.createElement( 'script' );
    if( callback ) script.onload = callback;
    script.type="text/javascript";
    script.src = url;
    document.body.appendChild( script );  
}

function loadMapsAPI() {
    addScript( 'https://maps.googleapis.com/maps/api/js?key=key_goes_here&sensor=false&callback=mapsApiReady' );
}

function mapsApiReady() {
    addScript( 'infobox.js', initialize );
}

window.onload = loadMapsAPI;

Leave a Comment