首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓谷歌地图V2 - Sd卡作为Tile供应商

安卓谷歌地图V2 - Sd卡作为Tile供应商
EN

Stack Overflow用户
提问于 2013-09-09 19:26:49
回答 1查看 3K关注 0票数 6

我正在使用Google V2开发一个android应用程序,我必须使用离线瓷砖,我在我的SD卡中拥有整个城市的所有瓷砖(来自开放街道地图的png格式)。我已经尝试过使用TileProvider接口,但没有起作用。我怎么能这么做?提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-10 20:13:20

我修改了一些东西,结果成功了。以下是代码:

CustomMapTileProvider.java

代码语言:javascript
复制
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实例

代码语言:javascript
复制
...

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

...
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18705481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档