Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript IE中的异步Google地图加载错误_Javascript_Google Maps_Asynchronous_Requirejs - Fatal编程技术网

Javascript IE中的异步Google地图加载错误

Javascript IE中的异步Google地图加载错误,javascript,google-maps,asynchronous,requirejs,Javascript,Google Maps,Asynchronous,Requirejs,我已经到处找了这个bug,但没有找到任何有效的。代码如下: MapsLoader.prototype._setup = function () { var promise = new RSVP.Promise(function (resolve) { // Callback for Google Maps that resolves the promise window.gmapsLoaded = function () { co

我已经到处找了这个bug,但没有找到任何有效的。代码如下:

MapsLoader.prototype._setup = function () {
    var promise = new RSVP.Promise(function (resolve) {

        // Callback for Google Maps that resolves the promise
        window.gmapsLoaded = function () {
            console.log('[Maps Loader] Loaded');
            resolve();
        };

        console.log('[Maps Loader] Loading...');

            // Create a URL with the API key pointing to the gmapsLoaded callback
        var mapsURL = 'https://maps.googleapis.com/maps/api/js?key=' + config.googleMaps.APIKey + '&sensor=false&callback=gmapsLoaded',

            // Create script object
            script = $('<script />').attr('src', mapsURL);

        $('head').append(script);
    });

    // Remove the gmapsLoaded function from global scope
    promise.then(function () {
        window.gmapsLoaded = undefined;
    });

    // Return the promise
    return promise;
};
这发生在“[Maps Loader]Loaded”日志消息之后,因此回调正在运行,页面继续正常加载,但Google Maps区域为空

如果我打开缓存(即不使用IE开发工具),页面有时会再次工作,这表明这可能是某种加载竞争条件

我可以通过在条件注释中的
head
中加载谷歌地图代码来修复它,因此它只会加载IE中的所有页面(然后检测
Google
是否已经存在于
MapsLoaded
模块中)。这确实有效,但并不令人愉快,我宁愿找到原因


其他信息:我使用RequireJS加载模块

我尝试过的事情:

  • 使用谷歌地图URL上的
    async=2
    参数(如建议)
  • 添加到
    主体
    而不是
    头部
    (与Google API示例一样)
  • 将所有内容放在
    $中。就绪
    调用

感谢使用
$。getScript(mapsURL)
(即通过AJAX请求加载它)而不是手动将脚本附加到
头上


不确定为什么以这种方式加载脚本会产生影响,但确实如此。

您可以将此脚本添加到项目的索引文件中。
该问题是由于项目中的polyfill支架不当造成的

通过快速连续两次向头部添加加载脚本(在有机会加载第一个脚本之前),Chrome获得了相同的结果查看IE开发者工具中的elements inspector,它加载在Google Maps
main.js
文件的两个副本中…如果我用
If(typeof Google=='undefined'){…}
包围Promise块中的所有内容,并在IE中打开缓存,所有内容都可以正常工作。
k.google.maps.Load(...)