如何在4.1版中为离线图块创建osmdroid XYTileSource?

时间:2014-04-03 15:44:01

标签: android offline osmdroid

我有使用4.0版的脱机osmdroid地图。升级到4.1,它们不再有效。我已将问题缩小到XYTileSource,其中aBaseUrl从4.0中的字符串更改为4.1中的数组。如何使脱机磁贴在4.1中工作?

旧的4.0代码有效。瓷砖位于/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);

我尝试将其更改为此,但它无法正常工作。

String[] urls = {"http://127.0.0.1"};
XYTileSource ts = new XYTileSource ( "tiles", 
                                      ResourceProxy.string.offline_mode, 
                                      13, 
                                      17, 
                                      256,
                                      ".png",
                                      urls);

2 个答案:

答案 0 :(得分:2)

我试着在这里提供一个完整的答案: Download maps for osmdroid

如果您有“旧”tiles.zip,请将其打开,然后检查:

  • 根目录名称=>把它作为XYTileSource构造函数的“aName”(它真的是“tile”吗?)
  • 图片图片extension =>把它作为aImageFileNameEnding(它真的是“.png”吗?)

aResourceId和aBaseUrl参数不用于zip文件。

答案 1 :(得分:2)

我发现您使用的是XYTileSource,默认情况下会扩展OnlineTileSourceBase

通过创建Url课程,我找到了CustomTileSource问题的解决方法。如下所示:

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

在我的代码中,我需要实例化tilesource,我使用:

 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

希望它有所帮助。