问题
I have offline osmdroid maps working using version 4.0. Upgrading to 4.1, they no longer work. I have narrowed the problem down to the XYTileSource, in which aBaseUrl changed from being a string in 4.0 to and array in 4.1. How do I get offline tiles to work in 4.1?
Old 4.0 code that worked. The tiles are in /sdcard/osmdroid/tiles.zip
XYTileSource ts = new XYTileSource ( "tiles",
ResourceProxy.string.offline_mode,
13,
17,
256,
".png",
"http://127.0.0.1");
mapView = (MapView) findViewById(R.id.mapview);
mapView.setTileSource(ts);
mapView.setMultiTouchControls(true);
mapView.setBuiltInZoomControls(false);
mapView.setUseDataConnection(false);
mapView.getController().setZoom(15);
GeoPoint point = new GeoPoint(40.715,-73.945);
mapView.getController().setCenter(point);
I tried changing it to this, but it doesn't work.
String[] urls = {"http://127.0.0.1"};
XYTileSource ts = new XYTileSource ( "tiles",
ResourceProxy.string.offline_mode,
13,
17,
256,
".png",
urls);
回答1:
I tried to provide a full answer here: Download maps for osmdroid
If you have an "old" tiles.zip, open it, and check:
- the root directory name => put it as the "aName" of XYTileSource constructor (is it really "tiles"?)
- the tiles images extension => put it as the aImageFileNameEnding (is it really ".png"?)
The aResourceId and aBaseUrl params are not used for zip files.
回答2:
I see that you are using XYTileSource
, which by default extends OnlineTileSourceBase
.
I have found a workaround for the Url
issue, by creating a CustomTileSource
class. Something like below:
public class CustomTileSource extends OnlineTileSourceBase {
public static String[] TILE_URL = {"my_url"};
//constructor is default - I changed nothing here
public CustomTileSource (String aName, string aResourceId, int aZoomMinLevel, int aZoomMaxLevel,
int aTileSizePixels, String aImageFilenameEnding, String[] url) {
super(
aName,
aResourceId,
aZoomMinLevel,
aZoomMaxLevel,
aTileSizePixels,
aImageFilenameEnding,
url);
// TODO Auto-generated constructor stub
}
/**
* 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();
}
}
In my code, where I need to instantiate the tilesource, I use:
CustomTileSource tileSource = new CustomTileSource ("Default", ResourceProxy.string.offline_mode, MIN_ZOOM_LVL, MAX_ZOOM_LVL, DEFAULT_TILE_SIZE, TILE_FORMAT, CustomTileSource.TILE_URL);
//MIN_ZOOM_LVL, MAX_ZOOM_LVL, DEFAULT_TILE_SIZE, TILE_FORMAT are constants that I defined elsewhere
Hope it helps.
来源:https://stackoverflow.com/questions/22842624/how-to-create-osmdroid-xytilesource-for-offline-tiles-in-version-4-1