如何使用osmdroid加载自定义图块

时间:2014-09-05 14:27:19

标签: android android-mapview openstreetmap osmdroid

我已使用Mobile Atlas creator提取地图上所选区域的地图图块。结果是zip文件包含文件夹(2,3,4,5,6 ...15),表示地图缩放。在每个文件夹中都有文件夹,在文件夹的侧面有图块。

我使用以下代码来读取包含切片的zip文件,但mapview上没有任何内容。有什么问题?

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

        // Specify the XML layout to use:
        setContentView(R.layout.activity_main);

        // Find the MapView controller in that layout:
        m_mapView = (MapView) findViewById(R.id.mapview);

        // Setup the mapView controller:
        m_mapView.setBuiltInZoomControls(true);
        m_mapView.setMultiTouchControls(true);
        m_mapView.setClickable(true);
        m_mapView.setUseDataConnection(false);
        m_mapView.getController().setZoom(MAP_DEFAULT_ZOOM);
        m_mapView.getController().setCenter(
                new GeoPoint(MAP_DEFAULT_LATITUDE, MAP_DEFAULT_LONGITUDE));

        // save zip to sd
        AssetManager assetManager = this.getAssets();
        InputStream is;
        String fileName = "Layer.zip"; // the zip file lies in assets root
        String path = this.getExternalFilesDir(null) + File.separator
                + fileName; // the path I save SD to

        File tileFile = new File(path);
        if (!tileFile.exists()) {
            try {
                is = assetManager.open(fileName);

                FileOutputStream fo = new FileOutputStream(path);

                byte[] b = new byte[1024];
                int length;
                while ((length = is.read(b)) != -1) {
                    fo.write(b, 0, length);
                }

                fo.flush();
                fo.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        IArchiveFile[] archives = new IArchiveFile[1];
        archives[0] = ArchiveFileFactory.getArchiveFile(tileFile);

        // Simple implementation that extends BitmapTileSourceBase and nothing
        // else
        CustomTileSource customTiles = new CustomTileSource("Maverik", null,
                10, 14, 256, ".png");

        MapTileModuleProviderBase[] providers = new MapTileModuleProviderBase[2];
        providers[0] = new MapTileFileArchiveProvider(
                new SimpleRegisterReceiver(getApplicationContext()),
                customTiles);
        //providers[1] = new MapTileDownloader(TileSourceFactory.MAPNIK);

        m_mapView.setUseDataConnection(true);

        MapTileProviderArray tileProvider = new MapTileProviderArray(
                customTiles, new SimpleRegisterReceiver(
                        this.getApplicationContext()), providers);

        TilesOverlay tilesOverlay = new TilesOverlay(tileProvider,
                getApplicationContext());
        tilesOverlay.setLoadingBackgroundColor(Color.TRANSPARENT);

        m_mapView.getOverlays().add(tilesOverlay);

        m_mapView.invalidate();

        // m_mapView.setTileSource(TileSourceFactory.MAPNIK);
    } // end onCreate()

} 

0 个答案:

没有答案