Skip to content

Instantly share code, notes, and snippets.

@thuytrinh
Created September 10, 2014 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thuytrinh/476f2800eb9bb66f7e1d to your computer and use it in GitHub Desktop.
Save thuytrinh/476f2800eb9bb66f7e1d to your computer and use it in GitHub Desktop.
Replace Google-based tile layer with MapBox-based tile layer
import com.google.android.gms.maps.model.UrlTileProvider;
import java.net.MalformedURLException;
import java.net.URL;
public class MapBoxTileProvider extends UrlTileProvider {
private String mBaseTileUrl;
private String mMapId;
private String mAccessToken;
/**
* Map ID and API access token can be obtained from https://www.mapbox.com/developers/
*/
public MapBoxTileProvider(String mapId, String accessToken) {
super(256, 256);
mBaseTileUrl = "http://api.tiles.mapbox.com/v4/%s/%d/%d/%d.png?access_token=%s";
mMapId = mapId;
mAccessToken = accessToken;
}
public URL getTileUrl(int x, int y, int z) {
try {
return new URL(String.format(mBaseTileUrl, mMapId, z, x, y, mAccessToken));
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}
}
}
map.setMapType(GoogleMap.MAP_TYPE_NONE);

String mapId = "<Your Map ID>";
String accessToken = "<Your API access token>";

TileOverlayOptions mapBoxTileOverlayOptions = new TileOverlayOptions();
mapBoxTileOverlayOptions.tileProvider(new MapBoxTileProvider(mapId, accessToken));
map.addTileOverlay(mapBoxTileOverlayOptions);

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment