arniotaki
arniotaki

Reputation: 2265

Android offline maps with pre-installed (on apk file) tiles

Is there a way to have an offline map without the need of first time network connection in order to cache some map data?

First I used Google maps but it seems that it only caches some of the tiles and it does not load anything the first time launch if you do not have internet connection

After that I tried osmdroid where it is close enough to google maps logic but I can't find a way to use it with assets folder and not with sdk folder pre-installed tiles. What if users tablet hasn't got an sdcard? I do need a global solution that will work on any tablet.

I need only the tiles for a country not the whole world!

Here is my code so far

  MapView mapView = new MapView(this, 256); //constructor

    mapView.setClickable(true);

    mapView.setBuiltInZoomControls(true);
    mapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);

    setContentView(mapView); //displaying the MapView

    mapView.getController().setZoom(6); //set initial zoom-level, depends on your need

    mapView.getController().setCenter(new GeoPoint(37.9513589,23.6474895)); //This point is in Enschede, Netherlands. You should select a point in your map or get it from user's location.

    mapView.setUseDataConnection(false);

and I have my tiles on assets/MapQuest/z/x/y.jpg

The weird is that in that link: https://github.com/osmdroid/osmdroid/wiki/Map-Sources it says that:

The Tile Source determines what imagery set is displayed, such as Bing, Mapquest, Mapnik, etc. The default Tile Provider, searches the following for your Tile Source, Assets, Offline zip/sqlite/etc in (/sdcard/osmdroid), Downloaded tile cache (/sdcard/osmdroid/tiles) and then finally the downloader.

Upvotes: 0

Views: 1366

Answers (1)

spy
spy

Reputation: 3258

Yes you can. Two strategies

  1. Prepare a sqlite file just like described in the wiki and place it in assets. When your app first starts, copy the database to the /sdcard/osmdroid/ before the map is recreated/inflated
  2. Store the contents of a tile zip in assets. The file structure would look like assets/TILESOURCENAME/Z/X/Y.extension. Using this mechanism, osmdroid droid should find it using the default tile provider chain.

Note: #1 is usually faster.

Upvotes: 0

Related Questions