Skip to content

Instantly share code, notes, and snippets.

@backflip
Forked from GFoley83/load-google-maps.js
Last active August 30, 2019 12:03
Show Gist options
  • Select an option

  • Save backflip/8613939 to your computer and use it in GitHub Desktop.

Select an option

Save backflip/8613939 to your computer and use it in GitHub Desktop.

Revisions

  1. backflip revised this gist Jan 25, 2014. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions bower.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    {
    "name": "load-google-maps",
    "version": "1.0.0",
    "main": ["./load-google-maps.js"],
    "author": "Glenn Baker & Gavin Foley",
    "dependencies": {
    "jquery": ">=1.5"
    },
    "license": ["MIT", "GPL"],
    "keywords": ["Google Maps", "Async"]
    }
  2. @GFoley83 GFoley83 revised this gist Jul 13, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sample-example.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    $(elem).on("myevent", function() {
    $.when( loadGoogleMaps( 3, API_KEY, LANG ) )
    $.when( loadGoogleMaps( 3, API_KEY, LANG, SENSOR ) )
    .then(function() { // or .done(...)
    !!google.maps // true
    });
  3. @GFoley83 GFoley83 revised this gist Jul 13, 2013. 1 changed file with 6 additions and 90 deletions.
    96 changes: 6 additions & 90 deletions sample-example.js
    Original file line number Diff line number Diff line change
    @@ -1,90 +1,6 @@
    /*!
    * JavaScript - loadGoogleMaps( version, apiKey, language, sensor )
    *
    * - Load Google Maps API using jQuery Deferred.
    * Useful if you want to only load the Google Maps API on-demand.
    * - Requires jQuery 1.5
    *
    * UPDATES by Gavin Foley
    * - Tidied JS & made it JSLint compliant
    * - Updated script request to Google Maps API to be protocol relative
    * - Added "sensor" parameter which defaults to false if not present
    *
    * Copyright (c) 2011 Glenn Baker
    * Dual licensed under the MIT and GPL licenses.
    */
    /*globals window, google, jQuery*/
    var loadGoogleMaps = (function ($) {
    "use strict";

    var now = $.now(),
    promise;

    return function (version, apiKey, language, sensor) {
    if (promise) {
    return promise;
    }

    //Create a Deferred Object
    var deferred = $.Deferred(),
    //Declare a resolve function, pass google.maps for the done functions
    resolve = function () {
    deferred.resolve(window.google && window.google.maps ? window.google.maps : false);
    },
    //global callback name
    callbackName = "loadGoogleMaps_" + (now++),

    // Default Parameters
    params = $.extend({
    "sensor": sensor || "false"
    },
    apiKey ? {
    "key": apiKey
    } : {},
    language ? {
    "language": language
    } : {});

    //If google.maps exists, then Google Maps API was probably loaded with the <script> tag
    if (window.google && window.google.maps) {
    resolve();
    //If the google.load method exists, lets load the Google Maps API in Async.
    } else if (window.google && window.google.load) {
    window.google.load("maps", version || 3, {
    "other_params": $.param(params),
    "callback": resolve
    });
    //Last, try pure jQuery Ajax technique to load the Google Maps API in Async.
    } else {
    //Ajax URL params
    params = $.extend(params, {
    'callback': callbackName
    });

    //Declare the global callback
    window[callbackName] = function () {
    resolve();

    //Delete callback
    setTimeout(function () {
    try {
    delete window[callbackName];
    } catch (e) {}
    }, 20);
    };

    //Can't use the jXHR promise because 'script' doesn't support 'callback=?'
    $.ajax({
    dataType: 'script',
    data: params,
    url: '//maps.googleapis.com/maps/api/js'
    });

    }

    promise = deferred.promise();

    return promise;
    };

    })(jQuery);
    $(elem).on("myevent", function() {
    $.when( loadGoogleMaps( 3, API_KEY, LANG ) )
    .then(function() { // or .done(...)
    !!google.maps // true
    });
    });
  4. @GFoley83 GFoley83 revised this gist Jul 13, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions load-google-maps.js
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ var loadGoogleMaps = (function ($) {

    // Default Parameters
    params = $.extend({
    'sensor': sensor ? sensor : "true"
    "sensor": sensor || "false"
    },
    apiKey ? {
    "key": apiKey
    @@ -52,7 +52,7 @@ var loadGoogleMaps = (function ($) {
    } else if (window.google && window.google.load) {
    window.google.load("maps", version || 3, {
    "other_params": $.param(params),
    "callback": resolve
    "callback": resolve
    });
    //Last, try pure jQuery Ajax technique to load the Google Maps API in Async.
    } else {
    @@ -69,7 +69,7 @@ var loadGoogleMaps = (function ($) {
    setTimeout(function () {
    try {
    delete window[callbackName];
    } catch (e) { }
    } catch (e) {}
    }, 20);
    };

  5. @GFoley83 GFoley83 revised this gist Jul 13, 2013. 1 changed file with 90 additions and 15 deletions.
    105 changes: 90 additions & 15 deletions sample-example.js
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,90 @@
    $(elem).on("myevent", function() {
    $.when( loadGoogleMaps( 3, API_KEY, LANG ) )
    .then(function() {
    !!google.maps // true
    });
    });

    // OR

    $(elem).on("myevent", function() {
    loadGoogleMaps( 3, API_KEY, LANG )
    .done(function() {
    !!google.maps // true
    });
    });
    /*!
    * JavaScript - loadGoogleMaps( version, apiKey, language, sensor )
    *
    * - Load Google Maps API using jQuery Deferred.
    * Useful if you want to only load the Google Maps API on-demand.
    * - Requires jQuery 1.5
    *
    * UPDATES by Gavin Foley
    * - Tidied JS & made it JSLint compliant
    * - Updated script request to Google Maps API to be protocol relative
    * - Added "sensor" parameter which defaults to false if not present
    *
    * Copyright (c) 2011 Glenn Baker
    * Dual licensed under the MIT and GPL licenses.
    */
    /*globals window, google, jQuery*/
    var loadGoogleMaps = (function ($) {
    "use strict";

    var now = $.now(),
    promise;

    return function (version, apiKey, language, sensor) {
    if (promise) {
    return promise;
    }

    //Create a Deferred Object
    var deferred = $.Deferred(),
    //Declare a resolve function, pass google.maps for the done functions
    resolve = function () {
    deferred.resolve(window.google && window.google.maps ? window.google.maps : false);
    },
    //global callback name
    callbackName = "loadGoogleMaps_" + (now++),

    // Default Parameters
    params = $.extend({
    "sensor": sensor || "false"
    },
    apiKey ? {
    "key": apiKey
    } : {},
    language ? {
    "language": language
    } : {});

    //If google.maps exists, then Google Maps API was probably loaded with the <script> tag
    if (window.google && window.google.maps) {
    resolve();
    //If the google.load method exists, lets load the Google Maps API in Async.
    } else if (window.google && window.google.load) {
    window.google.load("maps", version || 3, {
    "other_params": $.param(params),
    "callback": resolve
    });
    //Last, try pure jQuery Ajax technique to load the Google Maps API in Async.
    } else {
    //Ajax URL params
    params = $.extend(params, {
    'callback': callbackName
    });

    //Declare the global callback
    window[callbackName] = function () {
    resolve();

    //Delete callback
    setTimeout(function () {
    try {
    delete window[callbackName];
    } catch (e) {}
    }, 20);
    };

    //Can't use the jXHR promise because 'script' doesn't support 'callback=?'
    $.ajax({
    dataType: 'script',
    data: params,
    url: '//maps.googleapis.com/maps/api/js'
    });

    }

    promise = deferred.promise();

    return promise;
    };

    })(jQuery);
  6. @GFoley83 GFoley83 revised this gist Jul 8, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion load-google-maps.js
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    * - Requires jQuery 1.5
    *
    * UPDATES by Gavin Foley
    * - Tidied JS & made it JSLint complient
    * - Tidied JS & made it JSLint compliant
    * - Updated script request to Google Maps API to be protocol relative
    * - Added "sensor" parameter which defaults to false if not present
    *
  7. @GFoley83 GFoley83 revised this gist Jul 8, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion load-google-maps.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /*!
    * JavaScript - loadGoogleMaps( version, apiKey, language )
    * JavaScript - loadGoogleMaps( version, apiKey, language, sensor )
    *
    * - Load Google Maps API using jQuery Deferred.
    * Useful if you want to only load the Google Maps API on-demand.
  8. @GFoley83 GFoley83 revised this gist Jul 8, 2013. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions load-google-maps.js
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,11 @@
    * - Load Google Maps API using jQuery Deferred.
    * Useful if you want to only load the Google Maps API on-demand.
    * - Requires jQuery 1.5
    *
    * UPDATES by Gavin Foley
    * - Tidied JS & made it JSLint complient
    * - Updated script request to Google Maps API to be protocol relative
    * - Added "sensor" parameter which defaults to false if not present
    *
    * Copyright (c) 2011 Glenn Baker
    * Dual licensed under the MIT and GPL licenses.
  9. @GFoley83 GFoley83 revised this gist Jul 8, 2013. 1 changed file with 73 additions and 74 deletions.
    147 changes: 73 additions & 74 deletions load-google-maps.js
    Original file line number Diff line number Diff line change
    @@ -9,78 +9,77 @@
    * Dual licensed under the MIT and GPL licenses.
    */
    /*globals window, google, jQuery*/
    var loadGoogleMaps = (function($) {

    var now = $.now(),

    promise;

    return function( version, apiKey, language ) {

    if( promise ) { return promise; }

    //Create a Deferred Object
    var deferred = $.Deferred(),

    //Declare a resolve function, pass google.maps for the done functions
    resolve = function () {
    deferred.resolve( window.google && google.maps ? google.maps : false );
    },

    //global callback name
    callbackName = "loadGoogleMaps_" + ( now++ ),

    // Default Parameters
    params = $.extend(
    {'sensor': false}
    , apiKey ? {"key": apiKey} : {}
    , language ? {"language": language} : {}
    );;

    //If google.maps exists, then Google Maps API was probably loaded with the <script> tag
    if( window.google && google.maps ) {

    resolve();

    //If the google.load method exists, lets load the Google Maps API in Async.
    } else if ( window.google && google.load ) {

    google.load("maps", version || 3, {"other_params": $.param(params) , "callback" : resolve});
    var loadGoogleMaps = (function ($) {
    "use strict";

    //Last, try pure jQuery Ajax technique to load the Google Maps API in Async.
    } else {

    //Ajax URL params
    params = $.extend( params, {
    'v': version || 3,
    'callback': callbackName
    });

    //Declare the global callback
    window[callbackName] = function( ) {

    resolve();

    //Delete callback
    setTimeout(function() {
    try{
    delete window[callbackName];
    } catch( e ) {}
    }, 20);
    };

    //Can't use the jXHR promise because 'script' doesn't support 'callback=?'
    $.ajax({
    dataType: 'script',
    data: params,
    url: 'http://maps.google.com/maps/api/js'
    });

    }

    promise = deferred.promise();

    return promise;
    };

    }(jQuery));
    var now = $.now(),
    promise;

    return function (version, apiKey, language, sensor) {
    if (promise) {
    return promise;
    }

    //Create a Deferred Object
    var deferred = $.Deferred(),
    //Declare a resolve function, pass google.maps for the done functions
    resolve = function () {
    deferred.resolve(window.google && window.google.maps ? window.google.maps : false);
    },
    //global callback name
    callbackName = "loadGoogleMaps_" + (now++),

    // Default Parameters
    params = $.extend({
    'sensor': sensor ? sensor : "true"
    },
    apiKey ? {
    "key": apiKey
    } : {},
    language ? {
    "language": language
    } : {});

    //If google.maps exists, then Google Maps API was probably loaded with the <script> tag
    if (window.google && window.google.maps) {
    resolve();
    //If the google.load method exists, lets load the Google Maps API in Async.
    } else if (window.google && window.google.load) {
    window.google.load("maps", version || 3, {
    "other_params": $.param(params),
    "callback": resolve
    });
    //Last, try pure jQuery Ajax technique to load the Google Maps API in Async.
    } else {
    //Ajax URL params
    params = $.extend(params, {
    'callback': callbackName
    });

    //Declare the global callback
    window[callbackName] = function () {
    resolve();

    //Delete callback
    setTimeout(function () {
    try {
    delete window[callbackName];
    } catch (e) { }
    }, 20);
    };

    //Can't use the jXHR promise because 'script' doesn't support 'callback=?'
    $.ajax({
    dataType: 'script',
    data: params,
    url: '//maps.googleapis.com/maps/api/js'
    });

    }

    promise = deferred.promise();

    return promise;
    };

    })(jQuery);
  10. @gbakernet gbakernet renamed this gist Feb 8, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  11. @gbakernet gbakernet revised this gist Feb 8, 2012. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions example.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    $(elem).on("myevent", function() {
    $.when( loadGoogleMaps( 3, API_KEY, LANG ) )
    .then(function() {
    !!google.maps // true
    });
    });

    // OR

    $(elem).on("myevent", function() {
    loadGoogleMaps( 3, API_KEY, LANG )
    .done(function() {
    !!google.maps // true
    });
    });
  12. @gbakernet gbakernet revised this gist Jan 9, 2012. 1 changed file with 11 additions and 8 deletions.
    19 changes: 11 additions & 8 deletions load-google-maps.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /*!
    * JavaScript - loadGoogleMaps( version, apiKey ) - 16/02/2011
    * JavaScript - loadGoogleMaps( version, apiKey, language )
    *
    * - Load Google Maps API using jQuery Deferred.
    * Useful if you want to only load the Google Maps API on-demand.
    @@ -15,7 +15,7 @@ var loadGoogleMaps = (function($) {

    promise;

    return function( version, apiKey ) {
    return function( version, apiKey, language ) {

    if( promise ) { return promise; }

    @@ -30,8 +30,12 @@ var loadGoogleMaps = (function($) {
    //global callback name
    callbackName = "loadGoogleMaps_" + ( now++ ),

    //Ajax URL params
    params;
    // Default Parameters
    params = $.extend(
    {'sensor': false}
    , apiKey ? {"key": apiKey} : {}
    , language ? {"language": language} : {}
    );;

    //If google.maps exists, then Google Maps API was probably loaded with the <script> tag
    if( window.google && google.maps ) {
    @@ -41,17 +45,16 @@ var loadGoogleMaps = (function($) {
    //If the google.load method exists, lets load the Google Maps API in Async.
    } else if ( window.google && google.load ) {

    google.load("maps", version || 3, {"other_params": "sensor=false", "callback" : resolve});
    google.load("maps", version || 3, {"other_params": $.param(params) , "callback" : resolve});

    //Last, try pure jQuery Ajax technique to load the Google Maps API in Async.
    } else {

    //Ajax URL params
    params = $.extend({
    params = $.extend( params, {
    'v': version || 3,
    'sensor': false,
    'callback': callbackName
    }, apiKey ? {"key": apiKey} : {} );
    });

    //Declare the global callback
    window[callbackName] = function( ) {
  13. @gbakernet gbakernet revised this gist Sep 5, 2011. 1 changed file with 9 additions and 3 deletions.
    12 changes: 9 additions & 3 deletions load-google-maps.js
    Original file line number Diff line number Diff line change
    @@ -8,13 +8,17 @@
    * Copyright (c) 2011 Glenn Baker
    * Dual licensed under the MIT and GPL licenses.
    */

    /*globals window, google, jQuery*/
    var loadGoogleMaps = (function($) {

    var now = $.now();
    var now = $.now(),

    promise;

    return function( version, apiKey ) {

    if( promise ) { return promise; }

    //Create a Deferred Object
    var deferred = $.Deferred(),

    @@ -71,7 +75,9 @@ var loadGoogleMaps = (function($) {

    }

    return deferred.promise();
    promise = deferred.promise();

    return promise;
    };

    }(jQuery));
  14. @gbakernet gbakernet revised this gist Feb 18, 2011. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions load-google-maps.js
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@

    var loadGoogleMaps = (function($) {

    var now = jQuery.now();
    var now = $.now();

    return function( version, apiKey ) {

    @@ -20,7 +20,7 @@ var loadGoogleMaps = (function($) {

    //Declare a resolve function, pass google.maps for the done functions
    resolve = function () {
    deferred.resolve( google && google.maps ? google.maps : false );
    deferred.resolve( window.google && google.maps ? google.maps : false );
    },

    //global callback name
    @@ -30,12 +30,12 @@ var loadGoogleMaps = (function($) {
    params;

    //If google.maps exists, then Google Maps API was probably loaded with the <script> tag
    if( google && google.maps ) {
    if( window.google && google.maps ) {

    resolve();

    //If the google.load method exists, lets load the Google Maps API in Async.
    } else if ( google && google.load ) {
    } else if ( window.google && google.load ) {

    google.load("maps", version || 3, {"other_params": "sensor=false", "callback" : resolve});

    @@ -47,7 +47,7 @@ var loadGoogleMaps = (function($) {
    'v': version || 3,
    'sensor': false,
    'callback': callbackName
    }, apiKey ? {"key": apiKey} : {} )
    }, apiKey ? {"key": apiKey} : {} );

    //Declare the global callback
    window[callbackName] = function( ) {
  15. @gbakernet gbakernet created this gist Feb 15, 2011.
    77 changes: 77 additions & 0 deletions load-google-maps.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    /*!
    * JavaScript - loadGoogleMaps( version, apiKey ) - 16/02/2011
    *
    * - Load Google Maps API using jQuery Deferred.
    * Useful if you want to only load the Google Maps API on-demand.
    * - Requires jQuery 1.5
    *
    * Copyright (c) 2011 Glenn Baker
    * Dual licensed under the MIT and GPL licenses.
    */

    var loadGoogleMaps = (function($) {

    var now = jQuery.now();

    return function( version, apiKey ) {

    //Create a Deferred Object
    var deferred = $.Deferred(),

    //Declare a resolve function, pass google.maps for the done functions
    resolve = function () {
    deferred.resolve( google && google.maps ? google.maps : false );
    },

    //global callback name
    callbackName = "loadGoogleMaps_" + ( now++ ),

    //Ajax URL params
    params;

    //If google.maps exists, then Google Maps API was probably loaded with the <script> tag
    if( google && google.maps ) {

    resolve();

    //If the google.load method exists, lets load the Google Maps API in Async.
    } else if ( google && google.load ) {

    google.load("maps", version || 3, {"other_params": "sensor=false", "callback" : resolve});

    //Last, try pure jQuery Ajax technique to load the Google Maps API in Async.
    } else {

    //Ajax URL params
    params = $.extend({
    'v': version || 3,
    'sensor': false,
    'callback': callbackName
    }, apiKey ? {"key": apiKey} : {} )

    //Declare the global callback
    window[callbackName] = function( ) {

    resolve();

    //Delete callback
    setTimeout(function() {
    try{
    delete window[callbackName];
    } catch( e ) {}
    }, 20);
    };

    //Can't use the jXHR promise because 'script' doesn't support 'callback=?'
    $.ajax({
    dataType: 'script',
    data: params,
    url: 'http://maps.google.com/maps/api/js'
    });

    }

    return deferred.promise();
    };

    }(jQuery));