javascript - 使用 maplibre-gl-js 加载本地 .mbtiles
问题描述
我想用maplibre-gl-js加载一个本地 .mbtiles (带有矢量图块) (如果知道重要的话,在 Cordova 应用程序中)。据我了解,我应该使用addProtocol方法。当我得到我的console.log时效果很好,但我不知道如何加载瓷砖......
这是我的代码:
maplibregl.addProtocol('mbtiles', (params, callback) => {
console.log('I get this log.');
// but what to do here to get local mbtiles vector tiles loaded?
});
样式定义如下:
...
"sources": {
"openmaptiles": {
"type": "vector",
"url": "mbtiles://map/data/test.mbtiles"
}
},
...
任何帮助/提示表示赞赏:)
如果您需要更多信息,请随时询问。
PS 我之前使用过 mapbox-gl-cordova-offline并试图了解这个插件如何加载瓷砖,但我还无法弄清楚。
解决方案
这是我为实现这一目标所做的工作:
maplibregl.addProtocol("custom", (params, callback) => {
// tileBuffer = get a arrayBuffer of a specific tile using params.url
if (tileBuffer) {
callback(null, tileBuffer, null, null);
} else {
let message = `Tile is not in DB: ${params.url}`;
callback(new Error(message));
}
return { cancel: () => { } };
});
我正在使用cordova-sqlite-ext
以便能够在 Cordova 中使用预填充的数据库。样式有一个tiles: [custom://database-name/{z}/{x}/{y}]
以便知道要获取哪个图块。
值得注意的是,我addProtocol
专门为此目的将方法添加到了 maplibre。:-)
推荐阅读
- django - Django:UserCreationForm 没有得到自定义用户类
- php - 学说:拥有的一面和相反的一面
- oracle - 在 oracle sql 中使用正则表达式从表列中搜索数据
- python - 有条件地从 Python 字典中获取值的最佳实践
- python - 如何使用搜索栏抓取有关特定产品的信息
- java - 步骤冻结。当文件很大时,PoiItemreader 不会读取 excel 文件。有什么建议吗?
- ios - UITableView 高度在运行时不会通过 SnapKit 的按钮操作而改变
- python - 为什么当 2*2 的输出矩阵初始化为零并分配输出时,用于 2*2 矩阵加法的 python 程序为两行提供相同的输出?
- r - 标记用 grid.arrange 排列的图
- typescript - Is it possible to mimic webpack custom module resolution in TypeScript?