2012-11-01 88 views
1

我想在我的應用程序中使用osmdroid離線地圖使用,但我無法找到的東西是如何設置從/assets夾。 所以我已經得到了瓷磚(從Mobile Atlas Creator),放置在/assets/maps.zip ,我想是這樣的:如何使用資產作爲osmdroid離線地圖的來源

final ITileSource tileSource = 
    new XYTileSource("maps", null, 15, 17, 256, "png", "/assets"); 
mapview.setBuiltInZoomControls(true); 
mapview.setTileSource(tileSource); 
mapview.getController().setZoom(15); 
mapview.setUseDataConnection(false); 

..這似乎是錯誤的某種方式。那麼有誰能指出我該怎麼做?

回答

0

如果您需要通過AssetManager獲取輸入流,則資產路徑爲「file:/// android_asset /」。

+0

我試圖與替代資產,不工作:/ –

+0

檢查原始文件夾中。資產並不是我認爲的目的。但我不知道如果osmdroid確實支持這種地圖「注入」 –

+0

事實證明,它通常不會:/ –

0

好的,這個問題已在幾年前問過了,
但是仍然沒有關於如何從資產文件夾中使用離線地圖的明確信息。
所以這裏是我的解決方案。

  1. 使用Mobile Atlas Creator創建地圖。
    我用阿特拉斯格式= osmdroid郵政編碼和地圖源= OpenStreetMap的4UMaps.eu
  2. 解壓的地圖,把你的資產的文件夾:
    資產/地圖/ 14,其中14 - 例如是與相應的縮放文件夾級別(你可能會有幾個文件夾)
  3. 加入,我發現某處在互聯網上類:

    import android.content.res.AssetManager; 
    import android.graphics.drawable.Drawable;  
    import org.osmdroid.ResourceProxy.string; 
    import org.osmdroid.tileprovider.tilesource.BitmapTileSourceBase; 
    import org.osmdroid.tileprovider.util.StreamUtils; 
    
    import java.io.InputStream; 
    
    /** 
    * Custom tile source, 
    * used to load map tiles from the assets 
    */ 
    public class AssetsTileSource extends BitmapTileSourceBase { 
        private final AssetManager mAssetManager; 
    
        public AssetsTileSource(final AssetManager assetManager, final String aName, final string aResourceId, 
          final int aZoomMinLevel, final int aZoomMaxLevel, final int aTileSizePixels, 
          final String aImageFilenameEnding) { 
         super(aName, aZoomMinLevel, aZoomMaxLevel, aTileSizePixels, aImageFilenameEnding); 
         mAssetManager = assetManager; 
        } 
    
        @Override 
        public Drawable getDrawable(final String aFilePath) { 
         InputStream inputStream = null; 
         try { 
          inputStream = mAssetManager.open(aFilePath); 
          if (inputStream != null) { 
           final Drawable drawable = getDrawable(inputStream); 
           return drawable; 
          } 
         } catch (final Throwable e) { 
          // Tile does not exist in assets folder. 
          // Ignore silently 
         } finally { 
          if (inputStream != null) { 
           StreamUtils.closeStream(inputStream); 
          } 
         }  
         return null; 
        } 
    } 
    

/** 
* Map tile provider, loads tile from assets folder 
*/ 
public class MapTileFileAssetsProvider extends MapTileModuleProviderBase { 

    protected ITileSource mTileSource; 

    public MapTileFileAssetsProvider(final ITileSource pTileSource) { 
     super(OpenStreetMapTileProviderConstants.NUMBER_OF_TILE_FILESYSTEM_THREADS, OpenStreetMapTileProviderConstants.TILE_FILESYSTEM_MAXIMUM_QUEUE_SIZE); 

     mTileSource = pTileSource; 
    } 

    @Override 
    public boolean getUsesDataConnection() { 
     return false; 
    } 

    @Override 
    protected String getName() { 
     return "Assets Folder Provider"; 
    } 

    @Override 
    protected String getThreadGroupName() { 
     return "assetsfolder"; 
    } 

    @Override 
    protected Runnable getTileLoader() { 
     return new TileLoader(); 
    } 

    @Override 
    public int getMinimumZoomLevel() { 
     return mTileSource != null ? mTileSource.getMinimumZoomLevel() : Constants.MAP_ZOOM_ZOOM_MAX; 
    } 

    @Override 
    public int getMaximumZoomLevel() { 
     return mTileSource != null ? mTileSource.getMaximumZoomLevel() : Constants.MAP_ZOOM_ZOOM_MIN; 
    } 

    @Override 
    public void setTileSource(final ITileSource pTileSource) { 
     mTileSource = pTileSource; 
    } 

    private class TileLoader extends MapTileModuleProviderBase.TileLoader { 

     @Override 
     public Drawable loadTile(final MapTileRequestState pState) throws CantContinueException { 

      if (mTileSource == null) { 
       return null; 
      } 

      final MapTile pTile = pState.getMapTile(); 
      String path = mTileSource.getTileRelativeFilenameString(pTile); 

      Drawable drawable; 
      try { 
       drawable = mTileSource.getDrawable(path); 
      } catch (final LowMemoryException e) { 
       // low memory so empty the queue 
       throw new CantContinueException(e); 
      } 

      return drawable; 
     } 
    } 
} 
  • 添加常量:

    //地圖分鐘縮放級別
    公共靜態最終詮釋MAP_ZOOM_ZOOM_MIN = 12;
    // map max zoom level
    public static final int MAP_ZOOM_ZOOM_MAX = 14;
    //在資產中映射文件夾名稱
    public static final String MAP_ASSETS_FOLDER_NAME =「map」;

  • 正如您可能猜到的那樣,它們代表我們以前使用Mobile Atlas Creator創建的最小和最大縮放級別。

  • 最後,但並非最不重要,
    的執行代碼段:

    //使地圖不使用互聯網
    mBinding.mapView。setUseDataConnection(假);

    // Initializing the tile provider to use offline maps from the assets 
        // This will load for instance from /map/14/12345/12345.png 
        AssetsTileSource tileSource = new AssetsTileSource(
          getAssets(), 
          Constants.MAP_ASSETS_FOLDER_NAME, 
          ResourceProxy.string.offline_mode, 
          Constants.MAP_ZOOM_ZOOM_MIN, 
          Constants.MAP_ZOOM_ZOOM_MAX, 
          256, ".png"); 
    
        MapTileModuleProviderBase moduleProvider = new 
          MapTileFileAssetsProvider(tileSource); 
        SimpleRegisterReceiver simpleReceiver = new 
          SimpleRegisterReceiver(this); 
        MapTileProviderArray tileProviderArray = new 
          MapTileProviderArray(tileSource, simpleReceiver, new 
          MapTileModuleProviderBase[] { moduleProvider }); 
    
        mBinding.mapView.setTileProvider(tileProviderArray); 
    
        // not forget to invalidate the map on zoom 
        mBinding.mapView.setMapListener(new MapListener() { 
         @Override 
         public boolean onScroll(ScrollEvent scrollEvent) { 
          return false; 
         } 
    
         @Override 
         public boolean onZoom(ZoomEvent zoomEvent) { 
          mBinding.mapView.invalidate(); 
          return false; 
         } 
        }); 
    
  • 相關問題