Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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

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
谷歌地图JSON标记异步加载?_Json_Google Maps_Asynchronous_Google Maps Api 3 - Fatal编程技术网

谷歌地图JSON标记异步加载?

谷歌地图JSON标记异步加载?,json,google-maps,asynchronous,google-maps-api-3,Json,Google Maps,Asynchronous,Google Maps Api 3,我在这里找到了一些从JSON文件加载Google地图标记数据的代码: 它工作得很好,但我在开发人员控制台中收到一个警告: Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/. 我对这个问题做了一些研究,

我在这里找到了一些从JSON文件加载Google地图标记数据的代码:

它工作得很好,但我在开发人员控制台中收到一个警告:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.
我对这个问题做了一些研究,问题似乎是这段代码有
'async':false
。我试图将该值更改为
true
,但随后我的函数出现了另一个错误,该函数显示了与用户最接近的结果,因为该函数启动时标记数组为空

是否有可能使其异步工作?禁用异步有什么问题吗

var json = (function () { 
    var json = null; 
    $.ajax({ 
        'async': false, 
        'global': false, 
        'url': "js/hotels.json", 
        'dataType': "json", 
        'success': function (data) {
             json = data; 
         }
    });
    return json;
})();

async:false
这是个坏主意,因为它会冻结UI。当您将其更改为true时,该函数称为async,因此下一行JS是
返回json将为空,因为ajax仍在运行。您需要在
success()
之后返回或继续maps API已经有了一个通过JSON创建标记(和其他功能)的方法,请查看