最近、TileProvider コードをオフライン マップ タイル用に最適化しましたが、マップをロードした後、または CameraChange を実行した後、getTile が最大 1 分間 (場合によってはまったく) 呼び出されないという新しい問題が発生しました。
この方法で TileProvider を初期化します (これは、setUpMap()、onResume()、およびマップをリセットした場合やオフライン マップのオン/オフ設定が変更された場合に呼び出す関数内にあります):
if (mapsOn()) customOverlay = mMap.addTileOverlay(new TileOverlayOptions().tileProvider
(new MyTileProvider()));
(customOverlay として保存して、onStop() で customOverlay.clearTileCache() を呼び出せるようにします。そうしないと、メモリに問題があるようです。)
そして、私の TileProvider は、こちらの例に従って、zip ファイルからタイルを取得するように変更されました。
public class MyTileProvider implements TileProvider {
private final int TILE_WIDTH = 256;
private final int TILE_HEIGHT = 256;
private static final int BUFFER_SIZE = 16 * 1024;
private String pathToZipFile = MyConstants.TILE_PROVIDER_PATH_PREFIX;
private ZipResourceFile zipFile;
private String currentPath;
public MyTileProvider() {
}
public Tile getTile(int x, int y, int zoom) {
//debug code here shows this is not getting called when the maps are not loading.
//How do I force it to be called?
byte[] image = readTileImage(x, y, zoom);
return image == null ? NO_TILE : new Tile (TILE_WIDTH, TILE_HEIGHT, image);
}
private byte[] readTileImage(int x, int y, int zoom) {
InputStream in = null;
ByteArrayOutputStream buffer = null;
if (zipFile==null) initializeZipFile(x, zoom);
try {
in = zipFile.getInputStream(zoom+"/"+x+"/"+y+".png");
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 e1) {
e1.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) {}
}
}
public void initializeZipFile(int x, int zoom) {
//code to choose the correct zip file based upon the zoom level and x coordinate
}
}