Java – Multiple graph block layers on osmdroid

Multiple graph block layers on osmdroid… here is a solution to the problem.

Multiple graph block layers on osmdroid

Currently I’m using OSMdroid basemap to load a graph block data layer

final MapTileProviderBasic tileProvider = 
    new MapTileProviderBasic(getApplicationContext());
final ITileSource tileSource = 
    new XYTileSource("MyCustomTiles", null, 1, 16, 256, ".png",
            "http://a.url.to/custom-tiles/");
tileProvider.setTileSource(tileSource);
final TilesOverlay tilesOverlay = 
    new TilesOverlay(tileProvider, this.getBaseContext());
tilesOverlay.setLoadingBackgroundColor(Color.TRANSPARENT);
osmv.getOverlays().add(tilesOverlay);

Can I render multiple data layers on BaseMap, or can only display one data layer at a time?
I found this example for GoogleMaps, but haven’t found some example OSMdroid code that handles multiple tileSources at once

Solution

Yes, of course. You simply add another TilesOverlay to the map. Overlays (also tilesOverlays) are drawn continuously from the lowest index of the list (=0).
Here is an example:

//create the first tilesOverlay
final MapTileProviderBasic tileProvider = new MapTileProviderBasic(getApplicationContext());
final ITileSource tileSource = new XYTileSource("MyCustomTiles", null, 1, 16, 256, ".png",
        "http://a.url.to/custom-tiles/");
tileProvider.setTileSource(tileSource);
final TilesOverlay tilesOverlay = new TilesOverlay(tileProvider, this.getBaseContext());
tilesOverlay.setLoadingBackgroundColor(Color.TRANSPARENT);

create the second one
final MapTileProviderBasic anotherTileProvider = new MapTileProviderBasic(getApplicationContext());
final ITileSource anotherTileSource = new XYTileSource("MyCustomTiles", null, 1, 16, 256, ".png",
        "http://a.secondurl.to/custom-tiles/");
anotherTileProvider.setTileSource(anotherTileSource);
final TilesOverlay secondTilesOverlay = new TilesOverlay(anotherTileProvider, this.getBaseContext());
secondTilesOverlay.setLoadingBackgroundColor(Color.TRANSPARENT);

 add the first tilesOverlay to the list
osmv.getOverlays().add(tilesOverlay);

 add the second tilesOverlay to the list
osmv.getOverlays().add(secondTilesOverlay);

Related Problems and Solutions