[英]Android Google Maps V2 - Sd card as Tile Provider
I'm developing an android app using Google Maps API V2 and i have to use offline tiles, i have all tiles (from open street maps in png format) of my entire city in my SD Card. 我正在使用谷歌地图API V2开发一个Android应用程序,我必须使用离线图块,我在我的SD卡中拥有整个城市的所有图块(来自png格式的开放街道地图)。 I already tried to use TileProvider Interface but didn't work. 我已经尝试使用TileProvider接口,但没有工作。 How can i do that ? 我怎样才能做到这一点 ? Thanks in advance. 提前致谢。
I modified somethings and it worked. 我修改了一些东西并且有效。 Here is the code: 这是代码:
CustomMapTileProvider.java 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;
}
}
Add TileOverlay to your GoogleMap instance 将TileOverlay添加到您的GoogleMap实例
...
map.setMapType(GoogleMap.MAP_TYPE_NONE);
TileOverlayOptions tileOverlay = new TileOverlayOptions();
tileOverlay.tileProvider(new CustomMapTileProvider());
map.addTileOverlay(tileOverlay).setZIndex(0);
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.