Just wanted to leave it here: Post-Loading external Javascript like GoogleMaps etc

Hi there …

Just a factory for loading GoogleMaps. You can extend it. Just need to call once the init method … :wink:

myApp.factory('MapService', function(
  $cordovaNetwork,
  $http,
  $rootScope
) {
  this.init = function() {
    if ($cordovaNetwork.isOnline()) {
      loadGoogleMapsApi();
    }

    $rootScope.$on('$cordovaNetwork:online', function(event, networkState) {
      loadGoogleMapsApi();
    });
  }

  var loadGoogleMapsApi = function() {
		if (!window.google) {
      $http.get('https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true&libraries=places').then(
        function(response) {
          var scriptReference = document.createElement('script');
    	    scriptReference.setAttribute('type','text/javascript');
          scriptReference.appendChild(document.createTextNode(response.data));
          document.getElementsByTagName("head")[0].appendChild(scriptReference);
          
          // more instructions like instancing a map ...
        },
        function(error) { return $q.reject(error); }
      );
		}
	}

  return this;
});
1 Like

Many Thanks For This!!!

Happy happy joy joy :wink: