1

我的网站有许多页面显示带有一堆标记的谷歌地图,这里有一个例子

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

2 回答 2

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 文件中而不是内联,您可以不传递数组而不是有很多重复的内联代码。

于 2012-09-27T09:33:37.980 回答
1

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

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

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

于 2012-09-27T09:08:52.157 回答