简体   繁体   English

Leaflet 离线 Map:如何存储 map 以供离线使用?

[英]Leaflet Offline Map: How to store a map for offline usage?

I want to save an OSM map with specific boundaries for offline usage in my file system without any DB outside of the web browser.我想在没有 web 浏览器之外的任何数据库的情况下,在我的文件系统中保存具有特定边界的 OSM map 以供离线使用。 After that I want to render the map with Leaflet library while offline.之后,我想在离线时使用 Leaflet 库渲染 map。 Is that possible?那可能吗?

I see two main approaches for this:我看到了两种主要的方法:

  1. Using IndexedDB , WebSQL , Web Storage and/or related in-browser databases.使用IndexedDBWebSQLWeb 存储和/或相关的浏览器内数据库。

    From what I've seen, most developers prefer to rely on a library that wraps these technologies to offer consistent support across browsers (since not all browsers support all these technologies).据我所见,大多数开发人员更喜欢依赖封装这些技术的库来提供跨浏览器的一致支持(因为并非所有浏览器都支持所有这些技术)。 Let me quote MDN :让我引用MDN

    Note: IndexedDB API is powerful, but may seem too complicated for simple cases.注意:IndexedDB API 功能强大,但对于简单的情况可能看起来过于复杂。 If you'd prefer a simple API, try libraries such as localForage, dexie.js, ZangoDB, PouchDB, idb, idb-keyval, JsStore and lovefield that make IndexedDB more programmer-friendly.如果您更喜欢简单的 API,请尝试使用 localForage、dexie.js、ZangoDB、PouchDB、idb、idb-keyval、JsStore 和 lovefield 等库,这些库使 IndexedDB 对程序员更友好。

    In particular, an implementation of a PouchDB-backed Leaflet Tilelayer is available;特别是,支持 PouchDB 的 Leaflet Tilelayer的实现是可用的; but don't lat this prevent you from exploring other options.但不要因为这会阻止您探索其他选择。

  2. Using ServiceWorker s ability to store things on a browser's Cache .使用ServiceWorker将内容存储在浏览器的Cache上的能力。

    A ServiceWorker is able to capture all network requests that a webpage makes, including tiles (even if the tileservers reside on a different domain name). ServiceWorker能够捕获网页发出的所有网络请求,包括磁贴(即使磁贴服务器位于不同的域名上)。 Then, one can apply the known ServiceWorker -based caching techniques .然后,可以应用已知的基于ServiceWorker的缓存技术 After that, it should be even possible to send messages to the ServiceWorker to seed the cache (or just make enough fetch requests for the tiles).在那之后,甚至应该可以向ServiceWorker发送消息以播种缓存(或者只为切片发出足够的fetch请求)。

    I'm not aware (at the time of this writing) of any straightforward Leaflet integrations.我不知道(在撰写本文时)任何简单的 Leaflet 集成。 Implementation involves creating a manifest to point to the JS file with the ServiceWorker code, and an implementation of a cache mechanism .实现包括创建一个清单以指向带有ServiceWorker代码的 JS 文件,以及一个缓存机制的实现。

I suspect this may be an XY problem, where saving tiles in cache of the web browser from an external provider sounds like only one of many possible solutions for the initial objective of offline usage, and not having to use (or actually not to install ?) a database on client side.我怀疑这可能是一个 XY 问题,在 web 浏览器的缓存中从外部提供商处保存切片听起来只是离线使用的初始目标的许多可能解决方案之一,而不必使用(或实际上不安装? ) 客户端的数据库。

If that is the case, @IvanSanchez's Leaflet.TileLayer.MBTiles plugin could fit the bill: while the mbtiles file is technically a database, it is contained in a single portable file without requiring installation (it is an SQLite file).如果是这种情况,@IvanSanchez 的Leaflet.TileLayer.MBTiles 插件可以满足要求:虽然 mbtiles 文件在技术上是一个数据库,但它包含在一个可移植的文件中,无需安装(它是一个 SQLite 文件)。

The size could be over a browser cache, but if you have some control on the client, you can just copy it on file system.大小可能超过浏览器缓存,但如果您对客户端有一些控制,您可以将其复制到文件系统上。 Then you can have your HTML page opened from file system as well, or a local server, which would also be more inline with offline usage.然后,您也可以从文件系统或本地服务器打开 HTML 页面,这也更适合离线使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM