Created
November 1, 2010 06:48
-
-
Save Takazudo/657734 to your computer and use it in GitHub Desktop.
$.gMapLoader: load google maps API V3 asynchlonously
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* $.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