2

I've been using Mapbox's Android SDK for a prototype and was wondering if anyone knows how to use a vector tile map that is not from Mapbox's servers. For example, I want to load Mapzen or even OpenMapTiles vector tile maps using Mapbox's Android SDK without much difference versus loading Mapbox's default map styles.

jlvmoster
  • 41
  • 11

1 Answers1

4

You could do this using MapView#setStyleUrl.

First create mapzen.json in your assets/ directory with this simple style (be sure to replace YOUR_MAPZEN_API_KEY with your real key) https://mapzen.com/developers/sign_up

{
"version": 8,
"sources": {
"osm": {
    "type": "vector",
    "tiles": ["https://vector.mapzen.com/osm/all/{z}/{x}/{y}.mvt?api_key=[YOUR_MAPZEN_API_KEY]"]
}
},
"layers": [{
   "id": "background",
   "type": "background",
   "paint": {
       "background-color": "#41afa5"
    }
}, {
   "id": "water",
   "type": "fill",
   "source": "osm",
   "source-layer": "water",
   "filter": ["==", "$type", "Polygon"],
   "paint": {
   "fill-color": "#3887be"
}
}]
}

Then set the custom style on your MapView:

mapView.setStyleUrl("asset://mapzen.json");

And finally, load the map:

mapView.getMapAsync(new OnMapReadyCallback() {
  @Override
  public void onMapReady(MapboxMap mapboxMap) {
    //customize map
  }
});
user485193
  • 66
  • 2
  • Awesome! So is the .json file necessary because of Mapbox in particular or is it a third party thing? – jlvmoster Jul 05 '17 at 13:37
  • Hey, so I tried this out, but I am using a custom server using apache2 and php to host an offline map. I am using tileserver-php found [here](https://github.com/klokantech/tileserver-php). I replaced the url from the `tiles` key with my server's http address: `http://public_ip_address/tileserver/tileserver.php?/index.json?/mbtiles/united_states_of_america/{z}/{x}/{y}.pbf` but the app only displays the background color. Please help! – jlvmoster Jul 06 '17 at 19:09
  • 3
    There is no `setStyleUrl` in mapbox android. I am using version 8.2.1. – jknair0 Aug 12 '19 at 11:03
  • Here's the documentation @jknair0: https://docs.mapbox.com/android/maps/guides/styles/set-a-style/#load-a-style – Sidnei Bernardo Dec 02 '22 at 12:15