【问题标题】:Googlemaps API very slow when initialized from window.onload从 window.onload 初始化时,Googlemaps API 非常慢
【发布时间】:2011-10-27 11:27:14
【问题描述】:

为了避免 ie 7/8“未指定错误”,我最近将 JSON 驱动的 GoogleMaps v3 实现的初始化逻辑从 $(document).ready 之后的内联移到了从 window.onload() 触发的事件函数中。现在,曾经非常快速的加载现在需要 15-20 秒 + 才能加载。我知道 oninit 和 onload 之间存在一些细微的差异,但这似乎很极端。有什么想法吗?

$(document).ready(function(){

        var branchitems=[];
        var markers=[];
        var map="";

        window.onload = function() {
            var latlng = new google.maps.LatLng(59.5, -100.68);

            var myOptions = {
                                zoom: 3,
                                center: latlng,
                                mapTypeId: google.maps.MapTypeId.TERRAIN 
                            };

            map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

            PopulateMap(map);

        }

        function PopulateMap(map){
            ...  my logic for the JSON portion of the map ...
                    };

【问题讨论】:

    标签: javascript google-maps-api-3 onload init


    【解决方案1】:

    如果您正在同步加载 API,window.onload 是放置此内容的正确位置,否则您的代码可能会在 Maps API 下载之前执行。

    如果这需要 10 到 15 秒,那么您很可能也在加载大量其他资源。 window.onload 在所有内容都完成下载之前不会执行(例如脚本标签中的所有脚本)。

    一种解决方案可能是异步加载 Maps API V3(请参阅:http://code.google.com/apis/maps/documentation/javascript/basics.html#Async)。然后,您可以在 API 加载后立即开始构建地图。

    另一种解决方案是在用户访问网站后立即异步加载您不需要准备好的任何内容,或者从更快的位置加载您的资源(例如从 Google CDN 加载 jQuery,而不是从您自己的服务器)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 2018-03-27
      • 2021-10-06
      • 1970-01-01
      • 2012-10-06
      • 1970-01-01
      相关资源
      最近更新 更多