Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 1st, 2012  |  syntax: None  |  size: 2.03 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*!
  2.  * JavaScript - loadGoogleMaps( version, apiKey, language )
  3.  *
  4.  * - Load Google Maps API using jQuery Deferred.
  5.  *   Useful if you want to only load the Google Maps API on-demand.
  6.  * - Requires jQuery 1.5
  7.  *
  8.  * Copyright (c) 2011 Glenn Baker
  9.  * Dual licensed under the MIT and GPL licenses.
  10.  */
  11. /*globals window, google, jQuery*/
  12. var loadGoogleMaps = (function($) {
  13.        
  14.         var now = $.now(),
  15.        
  16.                 promise;
  17.        
  18.         return function( version, apiKey, language ) {
  19.                
  20.                 if( promise ) { return promise; }
  21.                
  22.                         //Create a Deferred Object
  23.                 var     deferred = $.Deferred(),
  24.                
  25.                         //Declare a resolve function, pass google.maps for the done functions
  26.                         resolve = function () {
  27.                                 deferred.resolve( window.google && google.maps ? google.maps : false );
  28.                         },
  29.                        
  30.                         //global callback name
  31.                         callbackName = "loadGoogleMaps_" + ( now++ ),
  32.                        
  33.                         // Default Parameters
  34.                         params = $.extend(
  35.                          {'sensor': false}
  36.                          , apiKey ? {"key": apiKey} : {}
  37.                          , language ? {"language": language} : {}
  38.                         );;
  39.                
  40.                 //If google.maps exists, then Google Maps API was probably loaded with the <script> tag
  41.                 if( window.google && google.maps ) {
  42.                        
  43.                         resolve();
  44.                
  45.                 //If the google.load method exists, lets load the Google Maps API in Async.
  46.                 } else if ( window.google && google.load ) {
  47.                
  48.                         google.load("maps", version || 3, {"other_params": $.param(params) , "callback" : resolve});
  49.  
  50.                 //Last, try pure jQuery Ajax technique to load the Google Maps API in Async.
  51.                 } else {
  52.                        
  53.                         //Ajax URL params
  54.                         params = $.extend( params, {
  55.                                 'v': version || 3,
  56.                                 'callback': callbackName
  57.                         });
  58.                        
  59.                         //Declare the global callback
  60.                         window[callbackName] = function( ) {
  61.                                
  62.                                 resolve();
  63.                                
  64.                                 //Delete callback
  65.                                 setTimeout(function() {
  66.                                         try{
  67.                                                 delete window[callbackName];
  68.                                         } catch( e ) {}
  69.                                 }, 20);
  70.                         };
  71.                        
  72.                         //Can't use the jXHR promise because 'script' doesn't support 'callback=?'
  73.                         $.ajax({
  74.                                 dataType: 'script',
  75.                                 data: params,
  76.                                 url: 'http://maps.google.com/maps/api/js'                              
  77.                         });
  78.                        
  79.                 }
  80.        
  81.                 promise = deferred.promise();
  82.                
  83.                 return promise;
  84.         };
  85.        
  86. }(jQuery));