Search code examples
javaandroidosmdroid

Multiple tile layers on osmdroid


Currently I am loading one tile data-layer over an OSMdroid basemap with

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);

Is it possible to render multiple data layers on top of each other over the BaseMap or can I only display one data layer at a time? I found this example for GoogleMaps, but haven't found some example OSMdroid code dealing with multipe tileSources at a time.


Solution

  • Yes, of course you can. You just have to add another TilesOverlay to the map. The overlays(also tilesOverlays) get drawn consecutively, starting at the list's lowest index(=0). Here's 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);