首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Osmdroid 5.6,脱机MBTiles

Osmdroid 5.6,脱机MBTiles
EN

Stack Overflow用户
提问于 2017-07-26 14:57:19
回答 1查看 1.1K关注 0票数 2

我正在尝试在osmdroid中离线使用MBtiles,

我从这里取了https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/java/org/osmdroid/samplefragments/tileproviders/SampleOfflineOnly.java的代码样本

但是总是显示空地图,我的代码有问题吗?

我的代码是:

代码语言:javascript
复制
public class OSMDroid extends AppCompatActivity {

private MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_osmdroid);

    mapView = (MapView) findViewById(R.id.map);


    String name = "map.mbtiles";
    File f = new File(Environment.getExternalStorageDirectory() + "/osmdroid", name);
    if (f.exists()) {
        try {

            //ok found a file we support and have a driver for the format, for this demo, we'll just use the first one

            //create the offline tile provider, it will only do offline file archives
            //again using the first file
            OfflineTileProvider tileProvider = new OfflineTileProvider(new SimpleRegisterReceiver(this),
                    new File[]{f});

            //tell osmdroid to use that provider instead of the default rig which is (asserts, cache, files/archives, online
            mapView.setTileProvider(tileProvider);

            //this bit enables us to find out what tiles sources are available. note, that this action may take some time to run
            //and should be ran asynchronously. we've put it inline for simplicity

            String source = "";
            IArchiveFile[] archives = tileProvider.getArchives();
            if (archives.length > 0) {
                //cheating a bit here, get the first archive file and ask for the tile sources names it contains
                Set<String> tileSources = archives[0].getTileSources();
                //presumably, this would be a great place to tell your users which tiles sources are available
                if (!tileSources.isEmpty()) {
                    //ok good, we found at least one tile source, create a basic file based tile source using that name
                    //and set it. If we don't set it, osmdroid will attempt to use the default source, which is "MAPNIK",
                    //which probably won't match your offline tile source, unless it's MAPNIK
                    source = tileSources.iterator().next();
                    mapView.setTileSource(FileBasedTileSource.getSource(source));
                } else {
                    mapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
                }

            } else {
                mapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
            }
            mapView.setUseDataConnection(false);
            mapView.setBuiltInZoomControls(true);
            IMapController mapController = mapView.getController();
            mapController.setZoom(10);
            GeoPoint startPt = new GeoPoint(61.5797,51.5997);
            mapController.setCenter(startPt);
            mapView.invalidate();
            return;
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}
}

MBTilesFileArchive getTileSources总是返回空字符串,实现如下:

代码语言:javascript
复制
public Set<String> getTileSources(){
    //the MBTiles spec doesn't store source information in it, so we can't return anything
    return Collections.EMPTY_SET;
}
EN

回答 1

Stack Overflow用户

发布于 2018-07-26 08:13:05

为了制作离线地图,你应该先添加图块。您可以使用Maperitive应用程序来制作地图切片(zip比sql更容易管理)。将该压缩文件命名为MapquestOSM。完成后,在手机内存中创建一个"osmdroid“文件夹(直接放入内存或SD卡),并在其中添加地图瓦片。

与您创建的地图分块相关的XYTileSource的参数更改。此代码处理有关地图分片本身的所有内容。我希望它能帮助你

代码语言:javascript
复制
mapView.setUseDataConnection(false);
mapView.setTileSource(new XYTileSource("MapquestOSM", 2, 15, 256, ".png", new String[]{}));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45319625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档