谷歌地图JSON标记异步加载?
我在这里找到了一些从JSON文件加载Google地图标记数据的代码: 它工作得很好,但我在开发人员控制台中收到一个警告:谷歌地图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/. 我对这个问题做了一些研究,
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创建标记(和其他功能)的方法,请查看