androidmapboxmbtiles

Code to Display the Tilemills mbtiles in android project


I am working on an android project in which i have to show the offline map using mbtiles . Is there any tutorial which shows how to set the mbtiles inandroid project .


Solution

  • sorry for being mr.Obvious, but it seems that www.google.com isn't your best friend...

    Anyways, lets be constructive:

    1.) You can use the MapBox SDK. Here is the link where is an example on how to use the online map. Now, that isn't of much use for the offline maps, but in there you can find the SDK. Just download it, and in there you can find also a test app where you can find out how the guys did it.

    I beleive there must be an easier way to install the library, but I had to import lots of new stuff to make this work. Also, be careful to get latest library jar's since I had okhttp-urlconnection-2.0.0.jar and the app crashed upon showing the view. And then I found out that I needed version 2.1.0 jar.

    2.) For offline map import I found this link helpful as it offers bits of code that eased my suffering to show the map properly. In case the link dies code states:

    To start create, for example, a method called setupMaboxOffline() in your WearActivity:

    public void setupMaboxOffline() {
        MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setZoom(5);
        mapView.setCenter(new LatLng(38.8977, -77.0365));
        mapView.setTileSource(new MBTilesLayer(this, "control-room-0.2.0.mbtiles"));
    }
    

    Then, call it within the WatchViewStub in onCreate(). This stub is used by Android Wear to choose the right layout (round or square) depending on the shape of the watch the user has.

    The MapView referenced above looks like this (in both layouts):

    <com.mapbox.mapboxsdk.views.MapView
        android:id="@+id/mapview
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    

    In case you'd need mapbox id property in the xml this SO question gets the answer.

    Basically, as I've only displayed the map, this is fairly simple... You need your .mbtiles file inside assets folder. Library needs to be showing no errors ;). In your xml file you've put the MapView custom view.

    Then in your Activity:

    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setTileSource(new MBTilesLayer(this, "map.mbtiles"));
    

    You can then add your custom settings like zoom and stuff.

    Good luck with your offline map. :)