Search code examples
javaandroidosmdroidmbtiles

Use MBTiles from online storage with OSMDroid


I have imported OSMDroid in my project and its running quite ok with default OpenStreetMap. I want to use an MBTiles format map instead of OSM. There are a lot of tutorial for using MBTiles as offline map but my MBTiles file is stored in an online storage such as

http://www.example.com/mymbtiles.mbtiles.

I am using the following code in my MainActivity.java which is xerox of default tutorial of OSMDroid

package com.example.user.osmdroid;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import org.osmdroid.api.IMapController;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.tileprovider.tilesource.XYTileSource;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;

public class MainActivity extends Activity {
    @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MapView map = (MapView) findViewById(R.id.map);
        map.setTileSource(TileSourceFactory.MAPNIK);
        map.setBuiltInZoomControls(true);
        map.setMultiTouchControls(true);
        IMapController mapController = map.getController();
        mapController.setZoom(9);
        GeoPoint startPoint = new GeoPoint(23.7588, 90.38967);
        mapController.setCenter(startPoint);
    }
}

How to use that online stored MBTiles file with my OSMDroid project?


Solution

  • You can't use it directly. MBTiles is basically an sqlite database. OSMDroid can read and use it from the device's storage but not over the network. To solve this, you'll have to transfer the database to the device, then tell OSMDroid to use it the mbtiles file via tile source and tile providers.

    Edit: considering the size of the database, the simplest thing for you to do is to host a simple web service that fetches the tiles from the database (server side) then provide them to your users via a custom ITileSource. I actually have code for such as service, but it can't be released (yet). The "standard" osm way is http://server/port:path/Z/X/Y.png but you can make it whatever format you want so long as there's a ITileSource setup on the android device. Also, you probably want to see to this example https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/java/org/osmdroid/samplefragments/SampleCustomTileSource.java Note, we're extending OnlineTileSourceBase