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 .
2 Answers
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. :)
-
Working further on this, I found something that might be helpful for others: When you set up mapView, you should also add: `mapView.setMinZoomLevel(1);` which is one of the requirements for the map display.. And also, one guy who was investigating the work of mbtiles, suggested that you should have more than one image size so your map displays properly. For me when I created an indoor offline map, it was blurry at some points. That's 'cause I used only one image.. Here's the [link](http://ahoj.io/creating-mbtiles-db-for-ios-mapbox-from-hi-res-map-image). – zed Nov 27 '14 at 17:10
-
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? – Devil's Dream Oct 19 '15 at 21:49
I've written a detailed tutorial on how to display local offline MBTiles files using Maplibre or Mapbox SDK over here -
https://medium.com/@ty2/how-to-display-offline-maps-using-maplibre-mapbox-39ad0f3c7543

- 4,217
- 5
- 26
- 46