gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:14:26 24 4
gpt4 key购买 nike

我的网站有许多页面显示带有一堆标记的 Google map ,here's an example .

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

我用来显示 map 和添加标记的代码附在下面。这是对 Google Maps V3 JavaScript API 的一种非常直接的用法,因此我认为没有太多的优化余地。我可以采取任何明显的步骤来减少 map 加载时间吗?

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();
});
}
};

最佳答案

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

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

关于javascript - 如何改善谷歌地图加载时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12617468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com