如何通过将地图图块图像保存到 sqlite 数据库中来使用 osmdroid 实现离线地图?
我有一个项目也需要在离线模式下显示地图数据。我也使用过 OpenStreet 地图。我已经保存了地图图像(图块),每个图块都由数据库中的图块键引用。我想从数据库访问这些地图图块并相应地使用它们。
请建议我。
提前致谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Osmdroid 中的地图图块由地图图块提供商提供。 Osmdroid 使用的默认图块提供程序是 MapTileProviderBasic。此提供程序扩展了 MapTileProviderArray ,这意味着它是几个其他图块提供程序的数组 - 当请求图块时,这些图块提供程序将被一一请求提供图块图像,直到其中一个提供为止。看一下
MapTileProviderBasic
的构造函数:提供程序数组中添加了三个地图切片提供程序,按以下顺序:
MapTileFilesystemProvider
- 提供来自文件系统(SD卡目录)MapTileFileArchiveProvider
- 提供来自文件系统中存档的图块MapTileDownloader
- 通过从互联网(例如从 OSM 服务器)下载图块来提供图块
MapTileProviderBasic
首先在文件系统中查找给定的图块,如果该图块不可用,则在存档文件中查找它,如果不可用,则再次从 Internet 下载该图块。好的,这是默认机制。如果您想更改此机制以查找存储在数据库中的图块,那么您可以创建自己的类似于 MapTileProviderBasic 的类。因此,您的类也可以扩展 MapTileProviderArray 并仅在构造函数中使用其他提供程序。在Osmdroid中有一个类DatabaseFileArchive 它可能可以帮助您从数据库中读取图块。
创建您自己的图块提供程序后,您应该使用它而不是默认的提供程序。地图图块提供程序附加到
MapView
。MapView
的某些构造函数将MapTileProviderBase
作为参数 - 您可以使用其中之一来附加您自己的提供程序。Map tiles in Osmdroid are provided by map tile providers. The default tile provider used by Osmdroid is MapTileProviderBasic. This provider extends MapTileProviderArray, which means that it is an array of a few other tile providers - when a tile is requested these tile providers are asked one by one for a tile image until one of them provides it. Take a look at the constructor of
MapTileProviderBasic
:There are three map tile providers added to the array of providers, in this order:
MapTileFilesystemProvider
- provides tiles from the file system (SD card directory)MapTileFileArchiveProvider
- provides tiles from archive in file systemMapTileDownloader
- provides tiles by downloading them from the Internet (e.g. from OSM servers)So the
MapTileProviderBasic
looks for a given tile first in the file system, if the tile is not available then it looks for it in archive files and again if it is not available there it downloads the tile from the Internet.Ok, this is the default mechanism. If you want to change this mechanism to look for tiles stored in a DB then you can create you own class similar to
MapTileProviderBasic
. So your class could also extendMapTileProviderArray
and just use other providers in the constructor. In Osmdroid there is a class DatabaseFileArchive which could probably help you in reading tiles from the DB.After creating your own tile provider you should use it instead of the default one. Map tile providers are attached to the
MapView
. Some of the constructors ofMapView
takeMapTileProviderBase
as an argument - you can use one of them to attach your own provider.让离线地图与默认提供程序一起使用的最简单方法
MapTileProviderBasic
是将您的地图档案放在OSMDROID_PATH
。换句话说,将
.zip、.sqlite、.mbtiles
或.gemf
文件下载到osmdroid/
目录中。如果您查看
MapTileFileArchiveProvider
你看到它在 getArchiveFiles() href="https://github.com/osmdroid/osmdroid/blob/master/osmdroid-android/src/main/java/org/osmdroid/tileprovider/modules/ArchiveFileFactory.java" rel="nofollow">ArchiveFileFactory
根据文件扩展名选择正确的存档提供程序。The simplest way to get offline maps to work with the default provider
MapTileProviderBasic
is to put your map arhive(s) inOSMDROID_PATH
.In other words, download your
.zip, .sqlite, .mbtiles
or.gemf
file into theosmdroid/
directory.If you look at
MapTileFileArchiveProvider
you see it callsgetArchiveFiles()
inArchiveFileFactory
which chooses the right archive provider based on the file extensions.