Javascript 如何使用开放层下载、存储和加载互动程序?

Javascript 如何使用开放层下载、存储和加载互动程序?,javascript,ecmascript-6,openlayers,Javascript,Ecmascript 6,Openlayers,我正在使用开放层创建一个PWA。用户必须具有在Wifi上下载互动程序的选项,以便离线加载互动程序。我阅读了OpenLayers文档,但找不到问题的答案。该部分为空。要使其正常工作,您需要三件事: 用于存储瓷砖的IndexedDB 用于平铺源的自定义平铺添加功能 为给定区段下载磁贴的组件 对于1,您需要建立一个商店,例如瓷砖。下面的代码段使用idb包: 从“idb”导入idb; let indexedDb; idb.openthis.name,1,upgradeDb=>{ if!upgradeDb

我正在使用开放层创建一个PWA。用户必须具有在Wifi上下载互动程序的选项,以便离线加载互动程序。我阅读了OpenLayers文档,但找不到问题的答案。该部分为空。

要使其正常工作,您需要三件事:

用于存储瓷砖的IndexedDB 用于平铺源的自定义平铺添加功能 为给定区段下载磁贴的组件 对于1,您需要建立一个商店,例如瓷砖。下面的代码段使用idb包:

从“idb”导入idb; let indexedDb; idb.openthis.name,1,upgradeDb=>{ if!upgradeDb.objectStoreNames.contains'tiles'{ upgradeDb.createObjectStore'tiles'; } }.thendb=>{ indexedDb=db; }; 对于2,起点可以如下所示:

source.setTileLoadFunctionTile,url{ const tx=db.transaction'tiles','readonly'; tiles=tx.objectStore'tiles'; const image=tile.getImage; tiles.geturl.thenblob=>{ 如果!滴{ //使用在线url image.src=url; 回来 } const objUrl=URL.createObjectURLblob; image.onload=函数{ URL.revokeObjectURLobjUrl; }; image.src=objUrl; }.catch=>{ //使用在线url image.src=url; }; } 对于3,您可能希望将下载限制在一个小范围内。然后,对于以地图单位表示的选定范围和要缓存的每个缩放级别,执行以下操作:

const tilegrid=source.getTileGrid; const projection=map.getView.getProjection; const getUrl=source.getTileUrlFunction; tilegrid.ForEachTileCordExtent、zoom、TileCord=>{ const url=geturltilecord,devicePixelRatio,投影; fetchurl.thenresponse=>{ 如果回答是肯定的{ response.blob.thenblob=>{ const tx=db.transaction'tiles','readwrite'; const tiles=tx.objectStore'tiles'; tiles.puturl,blob; }; } }; };
您需要三件事才能使其正常工作:

用于存储瓷砖的IndexedDB 用于平铺源的自定义平铺添加功能 为给定区段下载磁贴的组件 对于1,您需要建立一个商店,例如瓷砖。下面的代码段使用idb包:

从“idb”导入idb; let indexedDb; idb.openthis.name,1,upgradeDb=>{ if!upgradeDb.objectStoreNames.contains'tiles'{ upgradeDb.createObjectStore'tiles'; } }.thendb=>{ indexedDb=db; }; 对于2,起点可以如下所示:

source.setTileLoadFunctionTile,url{ const tx=db.transaction'tiles','readonly'; tiles=tx.objectStore'tiles'; const image=tile.getImage; tiles.geturl.thenblob=>{ 如果!滴{ //使用在线url image.src=url; 回来 } const objUrl=URL.createObjectURLblob; image.onload=函数{ URL.revokeObjectURLobjUrl; }; image.src=objUrl; }.catch=>{ //使用在线url image.src=url; }; } 对于3,您可能希望将下载限制在一个小范围内。然后,对于以地图单位表示的选定范围和要缓存的每个缩放级别,执行以下操作:

const tilegrid=source.getTileGrid; const projection=map.getView.getProjection; const getUrl=source.getTileUrlFunction; tilegrid.ForEachTileCordExtent、zoom、TileCord=>{ const url=geturltilecord,devicePixelRatio,投影; fetchurl.thenresponse=>{ 如果回答是肯定的{ response.blob.thenblob=>{ const tx=db.transaction'tiles','readwrite'; const tiles=tx.objectStore'tiles'; tiles.puturl,blob; }; } }; };
OpenLayers 2具有内置的localStorage缓存,用于缓存到会话存储或更新版本中的localStorage请参阅OpenLayers 2具有内置的localStorage缓存,用于缓存到会话存储或更新版本中的localStorage请参阅感谢您的回答。我还有一个问题:我能放在IndexDB上的数据有限制吗?是的,有限制,这些限制取决于几个因素。看,谢谢你的回答。我还有一个问题:我能放在IndexDB上的数据有限制吗?是的,有限制,这些限制取决于几个因素。看见