【问题标题】:How to create osmdroid XYTileSource for offline tiles in version 4.1?如何在 4.1 版中为离线磁贴创建 osmdroid XYTileSource?
【发布时间】:2014-04-03 15:44:01
【问题描述】:

我有使用 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);

【问题讨论】:

  • 我认为 aBaseUrl 更改无关,尤其是因为您没有使用在线服务器。您应该尝试逐步检查磁贴提供者,看看他们是否正在返回磁贴。
  • 在线提供商工作。这只是我使用的离线磁贴的相同代码,在 4.1 中不再适用。
  • 我想我正在放弃 osmdroid 并将 Leaflet 放在 webview 中。它的性能要好得多,而且它可以仅从文件系统使用离线切片,而不是在 zip 中。 osmdroid 在处理大型 zip 文件时遇到内存问题。

标签: android offline osmdroid


【解决方案1】:

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

如果你有一个“旧的”tiles.zip,打开它并检查:

  • 根目录名 => 把它作为 XYTileSource 构造函数的“aName”(真的是“tiles”吗?)
  • 瓷砖图像扩展名 => 将其作为 aImageFileNameEnding(真的是“.png”吗?)

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

【讨论】:

  • 是的,它被称为tiles.zip,并且在zip 中有一个名为tiles 的路径,例如/tiles/1/2/3.png。我尝试在路径中使用和不使用tiles目录。
【解决方案2】:

我看到您使用的是XYTileSource,默认情况下扩展OnlineTileSourceBase

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

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

希望对你有帮助。

【讨论】:

  • 请注意,在较新版本的 OSMDroid 中,OnlineTileSourceBase 在其构造函数中没有 aResourceId
  • my_url 有意义吗?如果是这样,当磁贴在设备上时应该是什么,例如,Tablet\osmdroid\tiles\Mapnik
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-29
  • 1970-01-01
相关资源
最近更新 更多