开发者

Googlemaps API very slow when initialized from window.onload

开发者 https://www.devze.com 2023-03-28 01:09 出处:网络
To avoid an ie 7/8 \"unspecified error\", I recently moved the initialization logic for a JSON powered GoogleMaps v3 implementation frominline following $(document).ready to within an event function t

To avoid an ie 7/8 "unspecified error", I recently moved the initialization logic for a JSON powered GoogleMaps v3 implementation from inline following $(document).ready to within an event function triggered from window.onload(). Now, what was once a very fast load is now taking 15-20 seconds + to load. I understand that there are some subtle differences between oninit and onload, but this seems extreme. Any 开发者_Go百科thoughts?

$(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 ...
                    };


window.onload is the right place to put this if you're loading the API synchronously, otherwise your code may execute before the Maps API has downloaded.

If this is taking 10 - 15 seconds then it is likely that you are loading a bunch of other resources as well. window.onload won't execute until everything has finished downloading (e.g. all scripts in script tags).

One solution may be to load Maps API V3 asynchronously (see: http://code.google.com/apis/maps/documentation/javascript/basics.html#Async). You can then start constructing your map as soon as the API has loaded.

An alternate solution is to asynchronously load anything that you don't need to be ready as soon as a user visits the site, or to load your resources from faster locations (e.g. load jQuery from the Google CDN instead of from your own server).

0

精彩评论

暂无评论...
验证码 换一张
取 消