Skip to content

Instantly share code, notes, and snippets.

@sunnycyk
Created February 20, 2014 04:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunnycyk/9106984 to your computer and use it in GitHub Desktop.
Save sunnycyk/9106984 to your computer and use it in GitHub Desktop.
Asynchronous Loading Google Map API
angular.module('googleMapAPI', [])
.config(function(googleMapServiceProvider) {
googeMapServiceProvider.setLang('zh-HK'); // Configuration that loading Google Map API in Traditional Chinese
})
.provider('googleMapService', function () {
var language = 'en-US';
function loadScript($document, callback, success) {
var scriptTag = $document.createElement('script');
scriptTag.id = "google-map-script";
scriptTag.type = "text/javascript";
scriptTag.src = 'https://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false&callback=mapReady&region=HK&language=' + language;
$document.getElementsByTagName('body')[0].appendChild(scriptTag);
}
this.setLang = function(lang) {
language = lang;
}
this.$get = ['$document', '$q', '$window', function($document, $q, $window) {
var deferred = $q.defer();
loadScript($document[0]);
$window.mapReady = (function(deferred) {
return function() {
deferred.resolve(google);
delete $window.mapReady
}
})(deferred);
return deferred.promise;
}];
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment