Offer packages of map tiles for offline use(提供地图瓦片包以供离线使用)
问题描述
我正在制作一个必须离线工作的网络应用程序.到目前为止一切正常,我的最后一步是将地图图块离线.幸运的是,我确切地知道用户需要访问地图的哪些区域,因此我不必允许缓存数百万个图块.
I'm making a web application that has to work offline. So far everything works and my last step is to take the map tiles offline. Luckily I know exactly what areas of the map will need to be accessible to users, so I don't have to allow caching of millions of tiles.
地图分为多个区域,因此我们的想法是将这些区域的图块作为可下载的包"提供.
The map is split into areas and so the idea is to offer the tiles for these areas as downloadable 'packages.'
例如,当我在线时,我会转到平铺包"页面,该页面提供多个区域的下载.我选择我感兴趣的区域,它会下载瓷砖,当我离线时,我可以使用这些瓷砖.我只需要大约 2 个缩放级别,一个用于快速导航,一个用于快速导航,另一个用于获取更多细节.
For instance, when I'm online, I go to the 'tile packages' page, which offers downloads for several areas. I choose the area which I'm interested in, it downloads the tiles, and when I go offline, I'm able to use these tiles. I only need about 2 zoom levels, one far out for quick navigation, and one more up close for more detail.
我正在使用传单来提供地图.有没有人必须做这样的事情并且可以给我一些指导?我真的不知道如何解决这个问题,这是最后一块拼图.
I'm using leaflet to serve up the map. Has anyone had to do something like this and could give me some guidance? I really just don't know how to even approach this, and it's the last piece of the puzzle.
推荐答案
这就是我想出的.我将地图的一个区域导入我的数据库.然后,我将此部分作为可下载的包提供.当用户下载包时,将查询数据库并以 JSON 格式返回与该区域关联的所有图块.图像存储为 blob.然后,我将这个图块数组传递给解析数据的自定义传单层.这是图层的代码:
So here's what I came up with. I import an area of the map to my database. I then offer this section as a downloadable package. When the user downloads the package, the database is queried and returns all tiles associated with that area in JSON format. The images are stored as blobs. I then pass this array of tiles to a custom leaflet layer which parses the data. Here's the code for the layer:
define([], function() {
L.TileLayer.IDBTiles = L.TileLayer.extend({
initialize: function(url, options, tiles) {
options = L.setOptions(this, options);
// detecting retina displays, adjusting tileSize and zoom levels
if (options.detectRetina && L.Browser.retina && options.maxZoom > 0) {
options.tileSize = Math.floor(options.tileSize / 2);
options.zoomOffset++;
if (options.minZoom > 0) {
options.minZoom--;
}
this.options.maxZoom--;
}
this._url = url;
var subdomains = this.options.subdomains;
if (typeof subdomains === 'string') {
this.options.subdomains = subdomains.split('');
}
this.tiles = tiles;
},
getTileUrl: function (tilePoint) {
this._adjustTilePoint(tilePoint);
var z = this._getZoomForUrl();
var x = tilePoint.x;
var y = tilePoint.y;
var result = this.tiles.filter(function(row) {
return (row.value.tile_column === x
&& row.value.tile_row === y
&& row.value.zoom_level === z);
});
if(result[0]) return result[0].value.tile_data;
else return;
}
});
});
这篇关于提供地图瓦片包以供离线使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:提供地图瓦片包以供离线使用
基础教程推荐
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01