如何改善谷歌地图加载时间?

时间:2012-09-27 08:47:57

标签: javascript performance google-maps

我的网站上有许多页面显示带有一堆标记的Google地图here's an example

正如您所看到的,地图需要很长时间才能加载,我正在寻找改进方法。我希望使用GeoWebCache来缓存服务器上的地图图块,但我被告知这会违反Google地图的使用条款。

我在下面附加了用于显示地图和添加标记的代码。这是Google Maps V3 JavaScript API非常简单的用法,所以我认为没有太多优化它的空间。我可以采取哪些明显的步骤来缩短地图加载时间吗?

SF.Map = function(elementId, zoomLevel, center, baseImageDir) {

    this._baseImageDir = baseImageDir;
    var focalPoint = new google.maps.LatLng(center.latitude, center.longitude);

    var mapOptions = {
        streetViewControl: false,
        zoom: zoomLevel,
        center: focalPoint,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        mapTypeControl: false
    };

    this._map = new google.maps.Map(document.getElementById(elementId), mapOptions);
    this._shadow = this._getMarkerImage('shadow.png');
};

SF.Map.prototype._getMarkerImage = function(imageFile) {
    return new google.maps.MarkerImage(this._baseImageDir + '/map/' + imageFile);
};


SF.Map.prototype._addField = function(label, value) {
    return "<span class='mapField'><span class='mapLabel'>" + label + ": </span><span class='mapValue'>" + value + "</span></span>";
};

/**
 * Add a marker to the map
 * @param festivalData Defines where the marker should be placed, the icon that should be used, etc.
 * @param openOnClick
 */
SF.Map.prototype.addMarker = function(festivalData, openOnClick) {

    var map = this._map;
    var markerFile = festivalData.markerImage;

    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(festivalData.latitude, festivalData.longitude),
        map: map,
        title: festivalData.name,
        shadow: this._shadow,
        animation: google.maps.Animation.DROP,
        icon: this._getMarkerImage(markerFile)
    });

    var bubbleContent = "<a class='festivalName' href='" + festivalData.url + "'>" + festivalData.name + "</a><br/>";
    var startDate = festivalData.start;
    var endDate = festivalData.end;

    if (startDate == endDate) {
        bubbleContent += this._addField("Date", startDate);

    } else {
        bubbleContent += this._addField("Start Date", startDate) + "<br/>";
        bubbleContent += this._addField("End Date", endDate);
    }

    // InfoBubble example page http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobubble/examples/example.html
    var infoBubble = new InfoBubble({
        map: map,
        content: bubbleContent,
        shadowStyle: 1,
        padding: 10,
        borderRadius: 8,
        borderWidth: 1,
        borderColor: '#2c2c2c',
        disableAutoPan: true,
        hideCloseButton: false,
        arrowSize: 0,
        arrowPosition: 50,
        arrowStyle: 0
    });

    var mapEvents = google.maps.event;

     // either open on click or open/close on mouse over/out
    if (openOnClick) {

        var showPopup = function() {
            if (!infoBubble.isOpen()) {
                infoBubble.open(map, marker);
            }
        };
        mapEvents.addListener(marker, 'click', showPopup);

    } else {

        mapEvents.addListener(marker, 'mouseover', function() {
            infoBubble.open(map, marker);
        });

        mapEvents.addListener(marker, 'mouseout', function() {
            infoBubble.close();
        });
    }
};

2 个答案:

答案 0 :(得分:2)

您可以尝试“延迟加载”谷歌地图,如下所示:

var script=document.createElement("script");
script.type="text/javascript";
script.async=true;
script.src="http://maps.google.com/maps/api/js?sensor=false&callback=handleApiReady";
document.body.appendChild(script);

或者就像这样我是如何使用Facebook AIP的,这样就不会减慢初始加载时间。

$.getScript('http://connect.facebook.net/en_US/all.js#xfbml=1', function() { 
           FB.init({appId: opt.facebook_id, status: true, cookie: true, xfbml: true}); 
}); 

示例:http://blog.rotacoo.com/lazy-loading-instances-of-the-google-maps-api

另外看你的HTML你有很多JS应该在外部JS文件而不是内联,你不能传递一个数组,而不是有很多重复的内联代码。

答案 1 :(得分:1)

一种选择是拍摄静态快照并在其后面创建地图。地图完全加载后,用静态地图替换动态地图。

我还建议你看看: https://developers.google.com/maps/documentation/staticmaps/

可能是您可以在不使用完整动态地图API的情况下为您的网站提供更简单的解决方案。