2

I have started to build an android apps using Mapbox Android SDK. I want to use my custom mbtiles file created from Tilemill. I was following this instruction

Code to Display the Tilemills mbtiles in android project

I have successfully implemented this code and working fine. But the problem, when I am building APK its becoming a huge file as my MBTiles file is too big. This is why its taking long to open the apps. Is there any way to get access the MBTiles file from online server storage such as http://www.example.com/mymap.mbtiles? I have tried the MapView Activity with the following code but didn't get any luck

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setZoom(10);
        mapView.setCenter(new LatLng(38.8977, -77.0365));
        mapView.setTileSource(new MBTilesLayer(this, "http://www.example.com/mymap.mbtiles"));

The XML file contains

<com.mapbox.mapboxsdk.views.MapView
    android:id="@+id/mapview
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
Community
  • 1
  • 1
Devil's Dream
  • 655
  • 3
  • 15
  • 38

2 Answers2

2

It seems to me the API you've used in your question is now deprecated in favor of the mapbox-gl-native project. However, from what I found in other answers here and on GitHub there might be a way to achieve what you want to do:

  1. Extract your .mbtiles file into .png tile files and a .json file using mb-util.
  2. Host these file on a server.
  3. Use TileJsonTileLayer instead of MBTilesLayer to access the online tiles. An attempt to do this can be found at issue #742 of the mapbox-android-sdk-legacy project.

If it doesn't work I'm sorry. This is the best I can come up with at the moment as I don't have the time to implement it myself.

jmeinke
  • 530
  • 6
  • 27
  • It works finally. But I have used WebSourceTileLayer instead of TileJsonTileLayer. The problem is tiles are loading too slowly. When I am using the same tiles with leaflet api in web framework, its not taking too long. Can you let me know the constructor of TileJsonTileLayer. Or is it replaced in 0.7.4 depreacated version? – Devil's Dream Nov 03 '15 at 06:26
  • 1
    I have found [TileJsonTileLayer.java](https://github.com/mapbox/mapbox-android-sdk-legacy/blob/0.7.4/MapboxAndroidSDK/src/main/java/com/mapbox/mapboxsdk/tileprovider/tilesource/TileJsonTileLayer.java) on the 0.7.4 branch of the legacy project. The TileJsonTileLayer should have the same constructor as WebSourceTileLayer. However, it seems to me you should use the method setTileJSON(JSONObject aTileJSON) or override getBrandedJSONURL to return the correct url. – jmeinke Nov 03 '15 at 11:28
  • ....Your solution is working but it is very hard time consuming and hectic work to export mbtiles through mb-util and then use it. Did you find any way to use mbTiles directly? – Devil's Dream Nov 05 '17 at 17:15
0

To display your own vector MBTiles file in Mapbox or Maplibre, you essentially just need to point to the MBTiles file as the source of the vector data in your style JSON.

In the sources tag of the style JSON, you need to replace the url value which might be pointing to another JSON to this format

mbtiles://<file-uri>

which may look like this

mbtiles://file:///data/user/0/com.example.maplibretest/cache/india_coimbatore.mbtiles

This will indicate the to the SDK to use this linked MBTiles file as the source of the vector data instead of a online source. In order for you to set this custom JSON as your style, you need to package a JSON in your app, copy it somewhere you can edit it, add the Uri of your MBTiles file to the url and then set that JSON file style to the Mapbox or Maplibre SDK. You can also fetch a style JSON from an online source, edit it, and then provide it to the SDK.

I've written about this whole process in detail in this blogpost - https://medium.com/@ty2/how-to-display-offline-maps-using-maplibre-mapbox-39ad0f3c7543

vepzfe
  • 4,217
  • 5
  • 26
  • 46