gpt4 book ai didi

java - Android Google Maps V2 - SD 卡作为 Tile Provider

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:40:41 25 4
gpt4 key购买 nike

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

最佳答案

我修改了一些东西并且它起作用了。这是代码:

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);

...

关于java - Android Google Maps V2 - SD 卡作为 Tile Provider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18705481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com