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();
}
}
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.