如何离线使用MOBAC创建的ODSMDroid SQLite瓦片源文件?[英] How to use MOBAC created OSMDroid SQLite tile source file offline?

本文是小编为大家收集整理的关于如何离线使用MOBAC创建的ODSMDroid SQLite瓦片源文件?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我一直在拉出我的头发,试图让我自己的离线移动地图集地图使用osmdroid 3.0.8没有运气的osmdroid 3.0.8.已经3天了.我会尝试用我的申请中的剪辑解释.我一直在延伸intemiediconoverlay和repossayitem,所以我希望它不会太令人困惑.

我创建了自己的osmdroid sqlite映射文件,其中3个不同的缩放级别为10平方公里.我将生成的"base.sqlite"文件复制到我的项目/res/原始/目录中.请注意,我的应用程序中的地理位点应该在地图的瓷砖范围内很好. "base.sqlite"文件应保存到应用程序特定数据目录.

接下来我在手机上调过了/osmdroid目录,所以我可以关闭以前缓存的地图.我以为我有自己的离线地图工作,直到我打开飞机模式,注意到缓存的地图仍然可用.

现在我所得到的就是空白.我没有Clue如何实现这一目标.我已经看到了几个例子,但经过大量的实验后,我没有成功获得任何工作.

private Hashtable<String, NodeOverlayItem> nodes = new Hashtable<String, NodeOverlayItem>();

private MapView mapView;
private Context context;

private LocationManager locManager;

private MapController mapController;
private MapTileProviderArray mapProvider;
private String mapTileArchivePath = "base.sqlite";

private ResourceProxy resourceProxy;

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        this.mapView = new MapView(this, 256);
        this.mapView.setBuiltInZoomControls(true);
        this.mapView.setMultiTouchControls(true);

        this.context = this.getApplicationContext();
        this.resourceProxy = new DefaultResourceProxyImpl(context);

        XYTileSource TILERENDERER = new XYTileSource("test", 
            ResourceProxy.string.offline_mode, 
            1, 20, 256, ".png", "http://127.0.0.1");
        SimpleRegisterReceiver simpleReceiver = new SimpleRegisterReceiver(this.context);

        IArchiveFile[] archives = { ArchiveFileFactory.getArchiveFile(this.getMapsFile()) };
        MapTileModuleProviderBase moduleProvider = new MapTileFileArchiveProvider(
                simpleReceiver, 
                TILERENDERER, 
                archives);
        this.mapProvider = new MapTileProviderArray(TILERENDERER, null, new MapTileModuleProviderBase[] { moduleProvider });
        this.mapProvider.setUseDataConnection(false);
        this.mapView = new MapView(this, 256, this.resourceProxy, this.mapProvider);
        this.mapView.setUseDataConnection(false);

        mapController = mapView.getController();
        mapController.setZoom(18);
        mapController.setCenter(new GeoPoint((int)(45.349622 * 1E6), (int)(-75.880700 *1E6)));

        this.setContentView(mapView);
    } catch(Exception ex) {
        Log.e("test", ex.getMessage());
    }    
}


public File getMapsFile() throws IOException {
    Log.d("test", "Trying to load map tiles to: " + this.mapTileArchivePath);
    FileOutputStream fos = this.openFileOutput(this.mapTileArchivePath, Context.MODE_PRIVATE);

    InputStream in = getResources().openRawResource(R.raw.osmdroid);
    byte[] buff = new byte[1024];
    int read = 0;
    try {
    while ((read = in.read(buff)) > 0) {
      fos.write(buff, 0, read);
    }
    } finally {
      in.close();
      fos.close();
    }
    return new File(this.getFilesDir(), this.mapTileArchivePath);
    }

推荐答案

好的!我知道我做错了什么,我现在都在工作! (我很兴奋:)

首先,我对应用程序特定目录写入我的原始资源映射文件(例如,OpenFileOutput())我使用了一个没有SD插槽的Galaxy Nexus,所以我无法转储地图文件到SD.与原始副本相比,确保您打算使用的Maps文件是字节. Eclipse的DDMS透视图是有用的,可用于查看设备的文件结构.

我还切换到OSMDroid Zip格式.然后,我确保XYTilesource()名称匹配Mobac在ZIP文件中创建的目录匹配,并确保瓦片大小和缩放级别匹配.

XYTileSource TILERENDERER = new XYTileSource("OSM CloudMade 1", ResourceProxy.string.offline_mode, 16, 18, 256, ".png", "http://127.0.0.1");

mobac默认情况下将创建256个像素图块.我创建了一个带16,17和18缩放级别的地图集文件. PNG是默认的Mobac瓷砖图像格式.

此外,如果您的地图文件有任何问题,甚至在maptilefilearchiveprive idear之前,acchivefilefactory.getArchiveFile()将捕获它们.

这是我的用法.只需尽一切努力正确地获得您的IARChive设置,您应该可以确定:

        XYTileSource TILERENDERER = new XYTileSource("OSM CloudMade 1", ResourceProxy.string.offline_mode, 16, 18, 256, ".png", "http://127.0.0.1");
        SimpleRegisterReceiver simpleReceiver = new SimpleRegisterReceiver(this.context);
        IArchiveFile[] archives = { ArchiveFileFactory.getArchiveFile(this.getMapsSdCard()) };
        MapTileModuleProviderBase moduleProvider = new MapTileFileArchiveProvider(
                simpleReceiver, 
                TILERENDERER, 
                archives);
        this.mapProvider = new MapTileProviderArray(TILERENDERER, null, new MapTileModuleProviderBase[] { moduleProvider });
        this.mapProvider.setUseDataConnection(false);
        this.mapView = new MapView(this, 256, this.resourceProxy, this.mapProvider);
        this.mapView.setUseDataConnection(false);

也许我是唯一一个遇到麻烦的人,但osmdroid没有明确记录如何做到这一点,当我打开问题时,我无法让他们评论我的用法.如果他们已表示我正在正确地实现MapTileFilearchiveProvider或包括一个良好的离线映射样本,我将首先专注于其他一切.

其他推荐答案

如果要使用sqlite db,则只需更改

ArchiveFileFactory.getArchiveFile(this.getMapsSdCard())

MBTilesFileArchive.getDatabaseFileArchive(f)

其中f是指向sqlite数据库的文件.

本文地址:https://www.itbaoku.cn/post/102609.html

问题描述

I've been pulling my hair out trying to get my own offline Mobile Atlas Creator OSMDroid SQLite map working with OSMDroid 3.0.8 without luck. It's been a long 3 days. I'll try to explain with clips from my application. I've been extending ItemizedIconOverlay and OverlayItem so I hope it doesn't get too confusing.

I created my own OSMDroid SQLite map file with 3 different zoom levels for a small are, like 10 square kms. I copied the resulting "base.sqlite" file into my project /res/raw/ directory. Note that the GeoPoints in my application should be well within the map's tile range. The "base.sqlite" file should get saved to the application specific data directory.

Next I turfed the /osmdroid directory on my phone so I could get the previously cached maps off. I thought I had my own offline maps working until I turned on Airplane mode and noticed the cached maps were still available.

Now all I get is blanks. I have no clue how to get this going. I've see a couple of examples but after a ton of experimentation I haven't been successful in getting any of them working.

private Hashtable<String, NodeOverlayItem> nodes = new Hashtable<String, NodeOverlayItem>();

private MapView mapView;
private Context context;

private LocationManager locManager;

private MapController mapController;
private MapTileProviderArray mapProvider;
private String mapTileArchivePath = "base.sqlite";

private ResourceProxy resourceProxy;

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        this.mapView = new MapView(this, 256);
        this.mapView.setBuiltInZoomControls(true);
        this.mapView.setMultiTouchControls(true);

        this.context = this.getApplicationContext();
        this.resourceProxy = new DefaultResourceProxyImpl(context);

        XYTileSource TILERENDERER = new XYTileSource("test", 
            ResourceProxy.string.offline_mode, 
            1, 20, 256, ".png", "http://127.0.0.1");
        SimpleRegisterReceiver simpleReceiver = new SimpleRegisterReceiver(this.context);

        IArchiveFile[] archives = { ArchiveFileFactory.getArchiveFile(this.getMapsFile()) };
        MapTileModuleProviderBase moduleProvider = new MapTileFileArchiveProvider(
                simpleReceiver, 
                TILERENDERER, 
                archives);
        this.mapProvider = new MapTileProviderArray(TILERENDERER, null, new MapTileModuleProviderBase[] { moduleProvider });
        this.mapProvider.setUseDataConnection(false);
        this.mapView = new MapView(this, 256, this.resourceProxy, this.mapProvider);
        this.mapView.setUseDataConnection(false);

        mapController = mapView.getController();
        mapController.setZoom(18);
        mapController.setCenter(new GeoPoint((int)(45.349622 * 1E6), (int)(-75.880700 *1E6)));

        this.setContentView(mapView);
    } catch(Exception ex) {
        Log.e("test", ex.getMessage());
    }    
}


public File getMapsFile() throws IOException {
    Log.d("test", "Trying to load map tiles to: " + this.mapTileArchivePath);
    FileOutputStream fos = this.openFileOutput(this.mapTileArchivePath, Context.MODE_PRIVATE);

    InputStream in = getResources().openRawResource(R.raw.osmdroid);
    byte[] buff = new byte[1024];
    int read = 0;
    try {
    while ((read = in.read(buff)) > 0) {
      fos.write(buff, 0, read);
    }
    } finally {
      in.close();
      fos.close();
    }
    return new File(this.getFilesDir(), this.mapTileArchivePath);
    }

推荐答案

OK! I know what I doing wrong and I have it all working now! (I'm excited :)

Firstly, I had some trouble with writing my Raw resource map file to the application specific directory (e.g. openFileOutput()) I'm using a Galaxy Nexus which doesn't have an SD slot so I can't dump the map file to SD. Ensure the maps file you intend to use is byte compared with the original copy. Eclipse's DDMS perspective is useful to view a device's file structure.

I also switched to the OSMdroid Zip format. I then made sure the XYTileSource() name matched the directory created in the Zip file by MOBAC, plus ensure the tile size and zoom levels match.

XYTileSource TILERENDERER = new XYTileSource("OSM CloudMade 1", ResourceProxy.string.offline_mode, 16, 18, 256, ".png", "http://127.0.0.1");

MOBAC by default will create 256 pixel tiles. I created an atlas file with 16, 17, and 18 zoom levels. PNG is the default MOBAC tile image format.

Also, if your map file has any issues, ArchiveFileFactory.getArchiveFile() will catch them, even before MapTileFileArchiveProvider.

Here's my usage. Just make every effort to get your IArchive setup correctly and you should be ok:

        XYTileSource TILERENDERER = new XYTileSource("OSM CloudMade 1", ResourceProxy.string.offline_mode, 16, 18, 256, ".png", "http://127.0.0.1");
        SimpleRegisterReceiver simpleReceiver = new SimpleRegisterReceiver(this.context);
        IArchiveFile[] archives = { ArchiveFileFactory.getArchiveFile(this.getMapsSdCard()) };
        MapTileModuleProviderBase moduleProvider = new MapTileFileArchiveProvider(
                simpleReceiver, 
                TILERENDERER, 
                archives);
        this.mapProvider = new MapTileProviderArray(TILERENDERER, null, new MapTileModuleProviderBase[] { moduleProvider });
        this.mapProvider.setUseDataConnection(false);
        this.mapView = new MapView(this, 256, this.resourceProxy, this.mapProvider);
        this.mapView.setUseDataConnection(false);

Maybe I'm the only one who had trouble with this, but osmdroid doesn't clearly document how to do this, and when I opened the issue I couldn't get them to comment on my usage. If they had said I was implementing MapTileFileArchiveProvider correctly or included a good offline mapping sample, I would have focused on everything else first.

其他推荐答案

If you want to use sqlite db you only have to change

ArchiveFileFactory.getArchiveFile(this.getMapsSdCard())

to

MBTilesFileArchive.getDatabaseFileArchive(f)

where f is a File that points to your sqlite database.