如何使用 OpenLayers 为日期动画预缓存图块
How to precache tiles with OpenLayers for date animation
我正在努力在一段时间内为具有多个图层的 OpenLayers 地图制作动画。我想预缓存 ol.layer.tile 个图块以在日期之间平滑过渡。关于如何 precache/preload 这些磁贴有什么建议吗?
您需要依靠您的浏览器缓存。并且它要求您的服务器发送适当的缓存 headers,因此浏览器不会 re-fetch 每个请求的图像。考虑到这些先决条件,请执行以下操作:
在您的源上调用 ol.source.TileImage#getTileUrlFunction
,这样您就可以计算要缓存的图块的 url。
在您的源上调用 ol.source.TileImage#getTileGrid
,以便您可以获得要缓存的范围和缩放级别的图块坐标
调用 ol.tilegrid.TileGrid#forEachTileCoord
函数计算每个图块的 url 并将其设置为图像 object 上的 src。执行此操作时,请跟踪加载图块的数量,以便您知道何时完成。
在对源进行相应的维度更改后,对所有维度重复上述操作。
最后,预加载单个维度的代码可能如下所示:
var loadingCount = 0;
var myTileUrlFunction = mySource.getTileUrlFunction();
var myTileGrid = mySource.getTileGrid();
myTileGrid.forEachTileCoord(myExtent, myZoom, function(tileCoord) {
var url = myTileUrlFunction.call(mySource, tileCoord, ol.has.DEVICE_PIXEL_RATIO, myProjection);
var img = new Image();
img.onload = function() {
--loadingCount;
if (loadingCount == 0) {
// ready to animate
}
}
++loadingCount;
img.src = url;
}
绕过cache-preventing的解决方案headers。
var i = 0;
var renderer = new ol.renderer.canvas.TileLayer(layer);
var tileSource = layer.getSource();
var datePromise = new Promise(function(resolve, reject) {
tileGrid.forEachTileCoord(extent, currentZ, function(tileCoord) {
tile = tileSource.getTile(tileCoord[0], tileCoord[1], tileCoord[2], pixelRatio, projection);
tile.load();
var loader = function(e) {
if(e.type === 'tileloadend') {
--i;
if(i === 0) {
resolve();
}
} else {
reject(new Error('No response at this URL'));
}
/* remove listeners */
this.un('tileloadend', loader);
this.un('tileloaderror', loader);
};
tileSource.on('tileloadend', loader);
tileSource.on('tileloaderror', loader);
++i;
});
});
我正在努力在一段时间内为具有多个图层的 OpenLayers 地图制作动画。我想预缓存 ol.layer.tile 个图块以在日期之间平滑过渡。关于如何 precache/preload 这些磁贴有什么建议吗?
您需要依靠您的浏览器缓存。并且它要求您的服务器发送适当的缓存 headers,因此浏览器不会 re-fetch 每个请求的图像。考虑到这些先决条件,请执行以下操作:
在您的源上调用
ol.source.TileImage#getTileUrlFunction
,这样您就可以计算要缓存的图块的 url。在您的源上调用
ol.source.TileImage#getTileGrid
,以便您可以获得要缓存的范围和缩放级别的图块坐标调用
ol.tilegrid.TileGrid#forEachTileCoord
函数计算每个图块的 url 并将其设置为图像 object 上的 src。执行此操作时,请跟踪加载图块的数量,以便您知道何时完成。在对源进行相应的维度更改后,对所有维度重复上述操作。
最后,预加载单个维度的代码可能如下所示:
var loadingCount = 0;
var myTileUrlFunction = mySource.getTileUrlFunction();
var myTileGrid = mySource.getTileGrid();
myTileGrid.forEachTileCoord(myExtent, myZoom, function(tileCoord) {
var url = myTileUrlFunction.call(mySource, tileCoord, ol.has.DEVICE_PIXEL_RATIO, myProjection);
var img = new Image();
img.onload = function() {
--loadingCount;
if (loadingCount == 0) {
// ready to animate
}
}
++loadingCount;
img.src = url;
}
绕过cache-preventing的解决方案headers。
var i = 0;
var renderer = new ol.renderer.canvas.TileLayer(layer);
var tileSource = layer.getSource();
var datePromise = new Promise(function(resolve, reject) {
tileGrid.forEachTileCoord(extent, currentZ, function(tileCoord) {
tile = tileSource.getTile(tileCoord[0], tileCoord[1], tileCoord[2], pixelRatio, projection);
tile.load();
var loader = function(e) {
if(e.type === 'tileloadend') {
--i;
if(i === 0) {
resolve();
}
} else {
reject(new Error('No response at this URL'));
}
/* remove listeners */
this.un('tileloadend', loader);
this.un('tileloaderror', loader);
};
tileSource.on('tileloadend', loader);
tileSource.on('tileloaderror', loader);
++i;
});
});