Skip to content

Instantly share code, notes, and snippets.

@k0d3d
Last active December 18, 2015 14:26
Show Gist options
  • Save k0d3d/79dcb9626dd2977a201e to your computer and use it in GitHub Desktop.
Save k0d3d/79dcb9626dd2977a201e to your computer and use it in GitHub Desktop.
var MapApp = angular.module('GoogleMapsInitializer', []);
MapApp.factory('Initializer', function($window, $q){
//Google's url for async maps initialization accepting callback function
var asyncUrl = 'https://maps.googleapis.com/maps/api/js?key=AIzaSyCOt9IYHpYN22m7alw_HKi5y5WBgu57p4s&v=3.exp&sensor=true&callback=googleMapsInitialized',
mapsDefer = $q.defer();
function init () {
var gMapsLoader = $.getScript(asyncUrl);
gMapsLoader.fail(function (err) {
return mapsDefer.reject(err);
});
//Callback function - resolving promise after maps successfully loaded
$window.googleMapsInitialized = mapsDefer.resolve;
}
init();
//Usage: Initializer.mapsInitialized.then(callback)
return {
mapsInitialized : mapsDefer.promise,
reload: init
};
});
MapApp.directive("cityState", ['Initializer', function (Initializer) {
return {
template: '<em>{{locationData.formatted_address}}</em>',
scope: {
locationData: "=cityState"
},
link: function (scope) {
Initializer.mapsInitialized
.then(function () {
});
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment