How can I implement offline maps using osmdroid by saving map tiles images into sqlite database?
Asked Answered
S

2

8

I have a project that has a requirement of displaying map data in offline mode also. I have used OpenStreet maps for the same. I have saved map images(tiles) and each tile is referenced by a tilekey in database. I want to access these map tiles from database and use them accordingly.

Please suggest me.

Thanks in advance.

Stinkweed answered 15/11, 2011 at 13:5 Comment(3)
can you elaborate how did you save those map tilesPericynthion
You can use mobac.sourceforge.net for downloading the map files to your android application and use the library like osmandroid or mapsforge to make use of the map that you downloadedUyekawa
and then.. which format that you use in mobac for creating the map? @AyushVermaBathtub
W
22

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:

public MapTileProviderBasic(final IRegisterReceiver pRegisterReceiver,
              final INetworkAvailablityCheck aNetworkAvailablityCheck, 
                                        final ITileSource pTileSource) {

    super(pTileSource, pRegisterReceiver);    
    final TileWriter tileWriter = new TileWriter();

    final MapTileFilesystemProvider fileSystemProvider = 
        new MapTileFilesystemProvider(pRegisterReceiver, pTileSource);
    mTileProviderList.add(fileSystemProvider);

    final MapTileFileArchiveProvider archiveProvider = 
        new MapTileFileArchiveProvider(pRegisterReceiver, pTileSource);
    mTileProviderList.add(archiveProvider);

    final MapTileDownloader downloaderProvider = 
        new MapTileDownloader(pTileSource, tileWriter, aNetworkAvailablityCheck);
    mTileProviderList.add(downloaderProvider);
}

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 system
  • MapTileDownloader - 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 extend MapTileProviderArray 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 of MapView take MapTileProviderBase as an argument - you can use one of them to attach your own provider.

Whittemore answered 27/11, 2011 at 14:34 Comment(3)
Thanks Piotr for your great effort. I am new in Android and I looked into jar file. I just found the above code and got the way, where i have to go. But I tried for 2 days and i coudnt proceed. It will be more more helpful if you provide a sample code for this. Thanks in advance Piotr.Stinkweed
@Piotr, can you please update the links in your answer as they are not working...Westminster
@Pallavi, Ok, links updated (osmdroid changed their repository structure).Whittemore
A
2

The simplest way to get offline maps to work with the default provider MapTileProviderBasic is to put your map arhive(s) in OSMDROID_PATH.

In other words, download your .zip, .sqlite, .mbtiles or .gemf file into the osmdroid/ directory.

If you look at MapTileFileArchiveProvider you see it calls getArchiveFiles() in ArchiveFileFactory which chooses the right archive provider based on the file extensions.

Antennule answered 15/12, 2014 at 12:55 Comment(7)
It just shows empty MapView. No Tile is displayed what to do.Epidiascope
@MaharithAdityaSS make sure you've positioned the map over an area your offline map covers, and also that the zoom-levels are within what your offline map coversAntennule
I wanna load Vector Tile Obtained from OpenMapTiles.org because of the Map Size. Is there any other way. Please Help Me out. I'm totally lost.Epidiascope
@MaharithAdityaSS If your budget allows it just get the example from here openmaptiles.org/docs/mobile/mobile - You really wanna switch to MapBox from OSMDroid anywaysAntennule
Is there any opensourceEpidiascope
Tell me any option which is open source. my requirement is simple. I just wanna load the map from SD card and which user places marker I have to get lat and long from it.Epidiascope
@MaharithAdityaSS you can use MapBox and check out their offline examples - they have a free version that serves up to 50.000 monthly active users. See mapbox.com/help/mobile-offline/#working-with-offline-caches. You can offline the maps when the user is online.Antennule

© 2022 - 2024 — McMap. All rights reserved.