【问题标题】:How to after json file already load and then load googlemap?如何在 json 文件已经加载然后加载 googlemap 之后?
【发布时间】:2017-03-14 01:21:14
【问题描述】:

我通过javascript回调函数使用googlemap

<script>
jsonmap={};
jqxhr=$.getJSON("postheat.json",function(data){
return data}).done(function(data){
  jsonmap=data;
  return data;
});
function initMap(){
   mapjson.....;
}
</script>
<script src="//maps.google.com/maps/api/js?sensor=true&callback=initMap"></script>

但是我发现当我 initMap 时,json 数据还没有准备好。如何确保json数据已经加载,然后执行initMap函数。

【问题讨论】:

    标签: javascript jquery json function webhooks


    【解决方案1】:

    done 块中调用完成后尝试调用initmap

    jsonmap={};
    
    jqxhr = $.getJSON("postheat.json",function(data){
                jsonmap=data;
                //try adding script to the page here:
                var s=document.createElement("script");
                s.src="//maps.google.com/maps/api/js?sensor=true&callback=initMap";
                document.head.appendChild(s);
            });
    
    function initMap(){
       mapjson.....;
    }
    

    但我确信有更好的方法。试试你是否可以从地图文档中获得一些东西。

    【讨论】:

    • 正如我所说,mapload 是回调,当mapload 完成时,它会直接调用initMap 函数。你的意思是我会调用两次iniMap?
    猜你喜欢
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    相关资源
    最近更新 更多