0

I don't understand the purpose of the aBaseUrl parameter in the class OnlineTileSourceBase. My reason for inquiring is that I am trying to get offline tiles to display and thus far can't get it to work. I see the overlay I created, but no map data (just that grey grid) and I wonder if I need to set aBaseUrl to something appropriate.

The data is on the device is in sdcard/osmdroid/tiles/Mapnik/. Mapnik contains folders 0, 1, ... 14, which themselves contains folders which contain .jpg files.

Online, this code works (removing the call setUseDataConnection(false) and setting tile source to MAPNIK). Based on code by @nightfixed here.

    public class MapActivity extends AppCompatActivity {

        final private int MIN_ZOOM_LEVEL = 0;
        final private int MAX_ZOOM_LEVEL = 14;
        final private int TILE_SIZE = 256;
        final private String IMAGE_EXTENSION = ".jpg";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            CustomTileSource  tileSource = new CustomTileSource ("Default",   
                    MIN_ZOOM_LEVEL,     
                    MAX_ZOOM_LEVEL,   
                    TILE_SIZE,                      
                    IMAGE_EXTENSION,  
                    CustomTileSource.TILE_URL);  

            final MapView mapView = (MapView) findViewById(R.id.mapview);
            mapView.setTileSource(tileSource);
            // mapView.setTileSource(TileSourceFactory.MAPNIK);
            mapView.setUseDataConnection(false); // keeps the mapView from loading online tiles using network connection.

        }

    }


   public class CustomTileSource extends OnlineTileSourceBase {

    public static String[] TILE_URL = {"my_url"};

    public CustomTileSource (String aName,
                             int aZoomMinLevel,
                             int aZoomMaxLevel,
                             int aTileSizePixels,
                             String aImageFilenameEnding,
                             String[] urlArray) {
        super(
                aName,
                aZoomMinLevel,
                aZoomMaxLevel,
                aTileSizePixels,
                aImageFilenameEnding,
                urlArray);

    }

    // returns the url for each tile, depending on zoom level
    // this is where I changed the return statement to take the first url from the string array of urls
    @Override
    public String getTileURLString(MapTile aTile) {
        return TILE_URL[0] + aTile.getX() + "+" + aTile.getY() + "+" + aTile.getZoomLevel();
    }
}
Community
  • 1
  • 1
Al Lelopath
  • 6,448
  • 13
  • 82
  • 139

2 Answers2

1

I suggest you to follow closely this post: Download maps for osmdroid

No need for CustomTileSource, just use mapView.setTileSource(TileSourceFactory.MAPNIK);

If your tiles are in "Mapnik" dir (sdcard/osmdroid/tiles/Mapnik) then TileSource aName should be set to "Mapnik", not to "Default".

When offline, aBaseUrl doesn't matter.

Community
  • 1
  • 1
MKer
  • 3,430
  • 1
  • 13
  • 18
  • In the post you link to, the url *http://otile1.mqcdn.com/tiles/1.0.0/map/* is used and it works for me (when in online mode). If I put that url in a browser, I get *Sorry - we haven't been able to serve the content you asked for*. Why is this? – Al Lelopath Dec 04 '15 at 16:55
  • because that's just the base url. osmdroid adds various parameters to get 1 specific tile at each request. – MKer Dec 04 '15 at 17:09
  • OIC, so yes, if I do *http://otile1.mqcdn.com/tiles/1.0.0/map/0/0/0.png* that I see the tile – Al Lelopath Dec 04 '15 at 17:21
  • great link, i just added most of the content to the osmdroid wiki. – spy Dec 05 '15 at 01:29
1

aBaseUrl is basically the primary url to the online map server. For example

http://tiles.mymapserver.com/mapdata (note, complete fictitious)

After the aBaseUrl, osmdroid calculates with tiles to load via various algorithms and then appends something like /Z/X/Y.jpg to the end of the aBaseUrl string when downloading.

spy
  • 3,199
  • 1
  • 18
  • 26