2017-03-20 45 views
0

我想加載放置在android studio的/ assets文件夾中的.map文件。 .map文件從mapsforge下載 @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);OSMDROID - 從mapsforge下載加載的.map文件

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」找不到。

+0

'文件f =新的文件( 「文件:/// android_asset /」 + 「myfile.map」);'。這不起作用,因爲File類無法處理來自資產的文件。 Google如何從資產中讀取文件。 – greenapps

+0

'if(f.exists())File [] list = f.listFiles();'。如果'f'是你的。地圖文件,那麼你怎麼能認爲你可以列出文件?你不是按照你所說的那樣加載那個文件,而是把它當作一個目錄來處理。 – greenapps

+0

我試着從InputStream中讀取資產,但在這裏我需要它是文件不inputStream,我應該讀取它後將inputStream轉換爲文件? – MJahongir

回答

0

如果OfflineTileProvider無法從InputStream讀取,但需要File對象到.map文件,那麼您應該首先關注真實文件。

因此,首先將所有'文件'從資產複製到文件系統上的文件。

之後,您可以使用這些文件。

將文件從資產複製到文件存儲的代碼已在此發佈了一百次。

+0

有一個.map文件 – MJahongir

+0

答案對一個文件也有效。你想說什麼? – greenapps

1

自從我寫你發佈的代碼,我想我在磬。

  1. 您發佈的代碼是真正加載離線瓦數據庫是osmdroid使用默認的瓷磚供應商鏈支持的集合( sqlite,mbtile,zip等)。由於這是一種特殊情況,因此Mapforge不在該列表中。
  2. 要使用地圖僞造osmdroid提供的瓷磚,你應該遵循這個例子https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/java/org/osmdroid/samplefragments/tileproviders/MapsforgeTileProviderSample.java我相信你會在那裏提供各種其他有用的例子。
  3. 綁定資產文件夾中的地圖文件或任何其他數據庫(雖然可以)但在osmdroid中加載這些瓷磚是不可能的,除了將原始jpg/png存儲在資產中。因此,如果捆綁資產是您選擇的路徑,那麼您需要將數據庫複製到設備存儲,然後告訴osmdroid加載它。我不確定爲什麼,但它可能與權限有關。

爲了節省您的時間,以下是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

thanx很多,我會檢查 – MJahongir

2

回答我的問題, * .MAP文件需要在/ SD卡/ 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; 
    

    }

+0

這適合你嗎? – JasonStack