Skip to content

Instantly share code, notes, and snippets.

@oleics
Last active August 29, 2015 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oleics/9529516 to your computer and use it in GitHub Desktop.
Save oleics/9529516 to your computer and use it in GitHub Desktop.
angular.module('demo', [])
.provider('googleMaps', require('./google-maps-service-provider'))
.config(['googleMapsProvider', function(googleMapsProvider) {
googleMapsProvider.configure({
key: 'your api key',
language: 'de',
libraries: 'places'
});
}])
.controller('MapController', ['googleMaps', function(googleMaps) {
googleMaps.then(function(googleMaps) {
// now we can extend google-maps-classes
function MarkerLabel_() {
}
inherits(MarkerLabel_, googleMaps.OverlayView);
});
}])
;
function inherits(childCtor, parentCtor) {
/** @constructor */
function tempCtor() {}
tempCtor.prototype = parentCtor.prototype;
childCtor.superClass_ = parentCtor.prototype;
childCtor.prototype = new tempCtor();
/** @override */
childCtor.prototype.constructor = childCtor;
}
module.exports = [GoolgMapsServiceProvider];
function GoolgMapsServiceProvider() {
// Some nice default options
this.options = {
// key: 'api-key here',
v: '3.15',
libraries: 'places',
language: 'en',
sensor: 'false'
};
// A function that lets us configure options of the service
this.configure = function(options) {
angular.extend(this.options, options);
};
// Return an instance of the service
this.$get = ['$q', function($q) {
return new GoolgMapsService($q, this.options);
}];
}
// The service, that is a promise for a reference to window.google.maps
function GoolgMapsService($q, options) {
var deferred = $q.defer();
// Early-resolve if google-maps-api is already in global-scope
if(typeof window.google !== 'undefined' && typeof window.google.maps !== 'undefined') {
deferred.resolve(window.google.maps);
return deferred.promise;
}
var randomizedFunctionName = options.callback = 'onGoogleMapsReady' + Math.round(Math.random()*1000);
window[randomizedFunctionName] = function() {
window[randomizedFunctionName] = null;
// Resolve the promise
deferred.resolve(window.google.maps);
};
var q = [];
angular.forEach(options, function(v, k) {
q.push(k+'='+v);
});
q = q.join('&');
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://maps.googleapis.com/maps/api/js?' + q;
document.body.appendChild(script);
// Return the promise
return deferred.promise;
}
@nmccready
Copy link

attempting to use this, it is not working.. any help is useful and appreciated

https://github.com/nlaplante/angular-google-maps/tree/master_maps_loader

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment