首页 文章

OSMDROID - 从mapsforge下载的.map文件

提问于
浏览
0

我正在尝试加载.map文件,它放在android studio的/ assets文件夹中 . .map文件是从mapsforge下载的@Override protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);的setContentView(R.layout.activity_main);

map = (MapView) findViewById(R.id.map);
    map.setBuiltInZoomControls(true);
    map.setTileSource(TileSourceFactory.MAPNIK);
    IMapController controller = map.getController();
    GeoPoint point = new GeoPoint(41.2585, 69.2097);
    controller.animateTo(point);
    controller.setZoom(10);

    File f = new File("file:///android_asset/" + "myfile.map");
    if (f.exists()) {
        File[] list = f.listFiles();
        if (list != null) {
            for (int i = 0; i < list.length; i++) {
                if (list[i].isDirectory()) {
                    continue;
                }
                String name = list[i].getName().toLowerCase();
                if (!name.contains(".")) {
                    continue; //skip files without an extension
                }
                name = name.substring(name.lastIndexOf(".") + 1);
                if (name.length() == 0) {
                    continue;
                }
                if (ArchiveFileFactory.isFileExtensionRegistered(name)) {
                    try {
                        OfflineTileProvider tileProvider = new OfflineTileProvider(new SimpleRegisterReceiver(this),
                                new File[]{list[i]});
                        map.setTileProvider(tileProvider);
                        String source = "";
                        IArchiveFile[] archives = tileProvider.getArchives();
                        if (archives.length > 0) {
                            Set<String> tileSources = archives[0].getTileSources();
                            if (!tileSources.isEmpty()) {
                                source = tileSources.iterator().next();
                                map.setTileSource(FileBasedTileSource.getSource(source));
                            } else map.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);

                        } else map.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
                        map.invalidate();
                        return;
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
        Toast.makeText(this, f.getAbsolutePath() + " did not have any files I can open! Try using MOBAC", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, f.getAbsolutePath() + " dir not found!", Toast.LENGTH_SHORT).show();
    }
}

找不到错误“myfileDirectory” .

3 回答

  • 1

    自从我写了你发布的代码后,我觉得我很喜欢 .

    • 您发布的代码实际上是用于加载osmdroid支持的默认磁贴提供程序链(sqlite,mbtile,zip等)的脱机磁贴数据库集合 . Mapsforge不在该列表中,因为它是一种特殊情况 .

    • 要使用 Map 伪造提供的瓷砖与osmdroid,你应该按照这个例子https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/java/org/osmdroid/samplefragments/tileproviders/MapsforgeTileProviderSample.java我有各种其他有用的例子 .

    • 捆绑 Map 文件或资产文件夹中的任何其他数据库(虽然它可以's possible to do) loading this tiles in osmdroid isn' t除了在资产中存储原始jpg / png之外 . 所以如果资产捆绑是你选择的路径,你'll want to copy the database to device storage, then tell osmdroid to load it. I'我不知道为什么但它可能要做有权限 .

    为了节省您的时间,这里是相关的代码片段,其中 mapsFile[] .

    XmlRenderTheme theme = null;
            try {
                theme = new AssetsRenderTheme(getContext().getApplicationContext(), "renderthemes/", "rendertheme-v4.xml");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
    
            fromFiles = MapsForgeTileSource.createFromFiles(maps, theme, "rendertheme-v4");
            forge = new MapsForgeTileProvider(
                new SimpleRegisterReceiver(getContext()),
                fromFiles, null);
    
    
            mMapView.setTileProvider(forge);
    
  • 0

    回答我自己的问题,* .map文件需要放在/ sdcard / osmdroid /下

    积分http://programtalk.com/java-api-usage-examples/org.osmdroid.mapsforge.MapsForgeTileProvider/

    在OnCreate里面......

    mMap = (MapView) findViewById(R.id.map);
        Set<File> mapfiles = findMapFiles();
        File[] maps = new File[mapfiles.size()];
        maps = mapfiles.toArray(maps);
        if (maps.length==0)Log.d(TAG, "No Mapsforge files found");
        else Toast.makeText(this, "Loaded " + maps.length + " map files",
       Toast.LENGTH_LONG).show(); 
        XmlRenderTheme theme = null;
        try {
            theme = new AssetsRenderTheme(getApplicationContext(), 
         "renderthemes/","rendertheme-v4.xml");
        }catch (Exception ex){
            ex.printStackTrace();
        }
        MapsForgeTileProvider forge = new MapsForgeTileProvider(new   
        SimpleRegisterReceiver(this), MapsForgeTileSource.createFromFiles(maps,  
        theme, "rendertheme-v4"), null);
        mMap.setTileProvider(forge);
        mMap.setUseDataConnection(false);
        mMap.setMultiTouchControls(true);
        mMap.setBuiltInZoomControls(true);
    
    • 方法获取.map文件
    protected static Set<File> findMapFiles() {
    Set<File> maps = new HashSet<>();
    List<StorageUtils.StorageInfo> storageList =   
    StorageUtils.getStorageList();
    for (int i = 0; i < storageList.size(); i++) {
        File f = new File(storageList.get(i).path + File.separator +  
    "osmdroid" + File.separator);
        if (f.exists()) {
            maps.addAll(scan(f));
        }
    }
    return maps;
    }
    
    static private Collection<? extends File> scan(File f) {
    List<File> ret = new ArrayList<>();
    File[] files = f.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            if (pathname.getName().toLowerCase().endsWith(".map"))
                return true;
            return false;
        }
    });
    if (files != null) {
        for (int i = 0; i < files.length; i++) {
            ret.add(files[i]);
        }
    }
    return ret;
    

    }

  • 1

    如果 OfflineTileProvider 无法从 InputStream 读取但需要 File 对象到.map文件,那么您应该首先关注真实文件 .

    因此,首先将所有“文件”从资产复制到文件系统上的文件 .

    之后,您可以使用这些文件 .

    将文件从资产复制到文件存储的代码已经在这里发布了一百次 .

相关问题