【问题标题】:Android Google Maps V2 - Sd card as Tile ProviderAndroid Google Maps V2 - Sd 卡作为 Tile Provider
【发布时间】:2013-09-09 19:26:49
【问题描述】:

我正在使用 Google Maps API V2 开发一个 android 应用程序,我必须使用离线图块,我的 SD 卡中有我整个城市的所有图块(来自 png 格式的开放街道地图)。 我已经尝试使用 TileProvider 接口但没有用。 我怎样才能做到这一点 ? 提前致谢。

【问题讨论】:

  • 就是这样。它在我的应用程序中以类似的方式为我工作。我猜你 getTileFilename 返回的路径是错误的。你试过写出来吗?您是否检查过是否获得了正确文件的路径?我的猜测是您应该在 getExternalStorageDirectory 上使用 getAbsolutePath 而不是 getPath,但这只是猜测。
  • @GuilhermeRuiz 你用过哪个open street maps?你是从哪里下载瓷砖的?
  • @UmerFarooq 我用了这个openstreetmap.org 并使用 MOBAC (mobac.sourceforge.net) 下载了瓷砖

标签: java android google-maps google-maps-android-api-2


【解决方案1】:

我修改了一些东西,它起作用了。代码如下:

CustomMapTileProvider.java

public class CustomMapTileProvider implements TileProvider {
    private static final int TILE_WIDTH = 256;
    private static final int TILE_HEIGHT = 256;
    private static final int BUFFER_SIZE = 16 * 1024;

    Override
    public Tile getTile(int x, int y, int zoom) {
        byte[] image = readTileImage(x, y, zoom);
        return image == null ? null : new Tile(TILE_WIDTH, TILE_HEIGHT, image);
    }

    private byte[] readTileImage(int x, int y, int zoom) {
        FileInputStream in = null;
        ByteArrayOutputStream buffer = null;

        try { in = new FileInputStream(getTileFile(x, y, zoom));
            buffer = new ByteArrayOutputStream();
            int nRead;
            byte[] data = new byte[BUFFER_SIZE];

            while ((nRead = in .read(data, 0, BUFFER_SIZE)) != -1) {
                buffer.write(data, 0, nRead);
            }
            buffer.flush();
            return buffer.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        } finally {
            if ( in != null)
                try { in .close();
                } catch (Exception ignored) {}
            if (buffer != null)
                try {
                    buffer.close();
                } catch (Exception ignored) {}
        }
    }

    private File getTileFile(int x, int y, int zoom) {
        File sdcard = Environment.getExternalStorageDirectory();
        String tileFile = "/TILES_FOLDER/" + zoom + '/' + x + '/' + y + ".png";
        File file = new File(sdcard, tileFile);
        return file;
    }
}

将 TileOverlay 添加到您的 GoogleMap 实例

...

map.setMapType(GoogleMap.MAP_TYPE_NONE);
TileOverlayOptions tileOverlay = new TileOverlayOptions();
tileOverlay.tileProvider(new CustomMapTileProvider());
map.addTileOverlay(tileOverlay).setZIndex(0);

...

【讨论】:

  • 你在构造函数中传递了 AssetManager 和 Context 而从不使用它们。
  • @Spartako 哦,谢谢!我正在使用 Context 进行一些测试,但我忘了删除它。
  • @GuilhermeRuiz,资产文件夹(tile 文件?)中应该放置什么,从哪里获取或下载?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-28
  • 1970-01-01
  • 2013-03-24
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多