Skip to content

Instantly share code, notes, and snippets.

@Takazudo
Created November 1, 2010 06:48
Show Gist options
  • Save Takazudo/657734 to your computer and use it in GitHub Desktop.
Save Takazudo/657734 to your computer and use it in GitHub Desktop.
$.gMapLoader: load google maps API V3 asynchlonously
/**
* $.gMapLoader
*/
$.GMapLoader = function(){};
$.GMapLoader.prototype = {
options: {
language: 'en',
sensor: false,
callbackProperty: 'gMapCallback'
},
load: function(options){
var data = $.extend({}, this.options, options);
window[data.callbackProperty] = data.callback || $.noop;
data.callback = 'window.' + data.callbackProperty;
delete data.callbackProperty;
data.sensor = data.sensor ? 'true' : 'false';
/*
Google maps tries to appendChild to document.body.
So if you call this in 'head', sometimes it fails.
This timer avoids it.
*/
var timer = setInterval(function(){
if(document.body){
$.ajax({
url: 'http://maps.google.com/maps/api/js',
dataType: 'script',
data: data
});
clearInterval(timer);
}
},10);
}
};
$.gMapLoader = new $.GMapLoader();
// usage:
// $.gMapLoader.load({
// language: 'ja', // 'en' default
// sensor: false,
// callback: function(){
// console.log('loaded');
// }
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment