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

如您所见,加载地图需要很长时间,我正在寻找改善方法。我希望使用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();
        });
    }
};

最佳答案

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

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文件中而不是内联,您是否可以不传递数组而不是拥有大量重复的内联代码?

10-07 19:38
查看更多