gpt4 book ai didi

google-maps-api-3 - 如何在 AngularJS 中异步加载谷歌地图?

转载 作者:行者123 更新时间:2023-12-04 16:48:17 25 4
gpt4 key购买 nike

现在我已经找到了一种在 Andy Joslin 的帮助下初始化 Google Maps 的方法。在这个 SO initialize-google-map-in-angularjs ,我正在寻找一种异步加载谷歌地图对象的方法。

我在 phonecat 中找到了如何执行此操作的示例。项目。

注意这个例子中 JS 文件是如何加载的:​​index-async.html

在加载到我的程序中的 Jade Scripts 部分中,我尝试了:

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']);
});

当我这样做并加载 map 页面时,我得到:
A call to document.write() from an asycrononously-loaded 
external script was ignored.

这就是现在将 Google map 作为服务加载的方式:
'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);
}

看来这应该是从我记得读过的内容中返回的一个 promise 。但是这个 AngularJS 对我来说很新。

最佳答案

这是我在不使用 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');
});
}
}
};
}]);

关于google-maps-api-3 - 如何在 AngularJS 中异步加载谷歌地图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11217002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com