Created
February 20, 2014 04:13
-
-
Save sunnycyk/9106984 to your computer and use it in GitHub Desktop.
Asynchronous Loading Google Map API
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
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®ion=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