5

Bây giờ tôi đã tìm thấy cách khởi tạo Google Maps với sự trợ giúp của Andy Joslin trong SO initialize-google-map-in-angularjs này, tôi đang tìm cách tải không đồng bộ đối tượng Google Map.Làm thế nào để tải không đồng bộ một bản đồ google trong AngularJS?

Tôi đã tìm thấy ví dụ về cách thực hiện việc này trong dự án phonecat.

Chú ý cách các tập tin JS được nạp trong ví dụ này: index-async.html

Trong Scripts Jade của tôi phần được nạp vào chương trình của tôi, tôi đã cố gắng:

script(src='js/lib/angular/angular.js') 
script(src='js/lib/script/script.min.js') 

script 
    $script([ 
    'js/lib/angular/angular-resource.min.js', 
    'js/lib/jquery/jquery-1.7.2.min.js', 
    'http://maps.googleapis.com/maps/api/js?key=AIzaSyBTmi_pcXMZtLX5MWFRQgbVEYx-h-pDXO4&sensor=false', 
    'js/app.js', 
    'js/services.js', 
    'js/controllers.js', 
    'js/filters.js', 
    'js/directives.js', 
    'bootstrap/js/bootstrap.min.js' 
    ], function() { 
     // when all is done, execute bootstrap angular application 
     angular.bootstrap(document, ['ofm']); 
    }); 

Khi tôi làm điều này và đi đến tải trang bản đồ tôi nhận được:

A call to document.write() from an asycrononously-loaded 
external script was ignored. 

này là làm thế nào Google Maps đang được nạp tại như một dịch vụ:

'use strict'; 

var app = angular.module('ofm.services', []); 

app.factory('GoogleMaps', function() { 

    var map_id = '#map'; 
    var lat  = 46.87916; 
    var lng  = -3.32910; 
    var zoom = 15; 
    var map  = initialize(map_id, lat, lng, zoom); 

    return map; 
}); 

function initialize(map_id, lat, lng, zoom) { 
    var myOptions = { 
    zoom : 8, 
    center : new google.maps.LatLng(lat, lng), 
    mapTypeId : google.maps.MapTypeId.ROADMAP 
    }; 
    return new google.maps.Map($(map_id)[0], myOptions); 
} 

Có vẻ như điều này sẽ trả về lời hứa từ những gì tôi nhớ lại. Nhưng AngularJS này rất mới đối với tôi.

+0

Để thúc đẩy tiến trình về điều này tôi đã tạo một dự án git tại đây: https://github.com/LarryEitel/angular-google-maps VÀ đã đẩy nó vào đây: http://angular-google-maps.nodester.com/. Tôi đã bắt đầu một chuỗi trong Google Groups về điều này tại đây: https://groups.google.com/forum/?fromgroups&nomobile=true#!topic/angular/CM8ewcWeTF4 –

+0

Khi bạn tải thư viện API Maps một cách không đồng bộ, bạn * phải * cung cấp chức năng gọi lại với tham số truy vấn 'callback ='. Nếu không trình nạp API sẽ sử dụng 'document.write()' không hoạt động từ một cuộc gọi không đồng bộ. Thư viện nhỏ trong câu trả lời của [GFoley83] (http://stackoverflow.com/a/17396353/1202830) thêm thông số này cho bạn, đó là lý do tại sao nó có thể hoạt động trong tình huống tải không đồng bộ như thế này. –

Trả lời

5

Nếu bạn sử dụng jQuery trong ứng dụng AngularJS của bạn, hãy kiểm tra chức năng này mà trả về một promise khi API của Google Maps đã được nạp:

https://gist.github.com/gbakernet/828536

tôi đã có thể sử dụng điều này trong một chỉ thị AngularJS để tải Google Maps theo yêu cầu. Làm việc một điều trị:

angular.module('mapModule') // usage: data-google-map 
    .directive('googleMap', ['$window', function ($window) { 
     return { 
      restrict: 'A', 
      link: function (scope, element, attrs) { 
       // If Google maps is already present then just initialise my map 
       if ($window.google && $window.google.maps) { 
        initGoogleMaps(); 
       } else { 
        loadGoogleMapsAsync(); 
       } 

       function loadGoogleMapsAsync() { 
        // loadGoogleMaps() == jQuery function from https://gist.github.com/gbakernet/828536 
        $.when(loadGoogleMaps()) 
         // When Google maps is loaded, add InfoBox - this is optional 
         .then(function() { 
          $.ajax({ url: "/resources/js/infobox.min.js", dataType: "script", async: false }); 
         }) 
         .done(function() { 
          initGoogleMaps(); 
         }); 
       }; 

       function initGoogleMaps() { 
        // Load your Google map stuff here 
        // Remember to wrap scope variables inside `scope.$apply(function(){...});` 
       } 
      } 
     }; 
    }]); 
+9

Eek, kết hợp Angular và jQuery! –

+1

@ Củ cải đường-Củ cải đường Đặt cược mông của bạn tôi đã làm! Hiện tại, không có cách nào dễ dàng để tải tập lệnh bằng AngularJS và API Google Maps không hỗ trợ lời hứa một cách tự nhiên. Tôi đã đề xuất một giải pháp thỏa đáng hơn; nếu bạn có một cái tốt hơn thì hãy xem nó! :) – GFoley83

+0

GFoley, tôi lấy ý kiến ​​của bạn và tôi đã không nói điều này là không hợp lý, tôi chỉ nói "eek". Tôi đã không cố gắng nhưng có thể $ AngularJS không ép buộc jQuery hứa hẹn vào riêng của mình theo Q.js? Nếu vậy, sau đó trong 'loadGoogleMapsAsync()', tất cả nhưng biểu thức '$ .ajax (...)' có thể được viết bằng $ q, có vẻ thích hợp hơn trong mô-đun Angular.js. –

7

đây là giải pháp của tôi, tôi đã đưa ra mà không sử dụng jQuery: (Gist here)

angular.module('testApp', []). 
    directive('lazyLoad', ['$window', '$q', function ($window, $q) { 
     function load_script() { 
      var s = document.createElement('script'); // use global document since Angular's $document is weak 
      s.src = 'https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize'; 
      document.body.appendChild(s); 
     } 
     function lazyLoadApi(key) { 
      var deferred = $q.defer(); 
      $window.initialize = function() { 
       deferred.resolve(); 
      }; 
      // thanks to Emil Stenström: http://friendlybit.com/js/lazy-loading-asyncronous-javascript/ 
      if ($window.attachEvent) { 
       $window.attachEvent('onload', load_script); 
      } else { 
       $window.addEventListener('load', load_script, false); 
      } 
      return deferred.promise; 
     } 
     return { 
      restrict: 'E', 
      link: function (scope, element, attrs) { // function content is optional 
      // in this example, it shows how and when the promises are resolved 
       if ($window.google && $window.google.maps) { 
        console.log('gmaps already loaded'); 
       } else { 
        lazyLoadApi().then(function() { 
         console.log('promise resolved'); 
         if ($window.google && $window.google.maps) { 
          console.log('gmaps loaded'); 
         } else { 
          console.log('gmaps not loaded'); 
         } 
        }, function() { 
         console.log('promise rejected'); 
        }); 
       } 
      } 
     }; 
    }]); 
2

Hãy xem những điều này tôi nghĩ rằng nó đáng tin cậy hơn

var deferred = $q.defer(); 
         var script = document.createElement('script'); 

         $window.initMap = function() { 
          //console.log("Map init "); 

          deferred.resolve(); 
         } 
         script.src = "//maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places&callback=initMap"; 
         document.body.appendChild(script); 
         return deferred.promise; 
Các vấn đề liên quan