高德地图加载本地瓦片图层LocalTileProvider

时间:2025-02-07 16:15:11
import .;
import .;

public  class LocalTileTileProvider implements TileProvider {

    private static final int TILE_WIDTH = 256;
    private static final int TILE_HEIGHT = 256;
    public static final int BUFFER_SIZE = 16 * 1024;

    private String tilePath;
    private byte[] tile;

    public LocalTileTileProvider(String path) {
        tilePath=path;
    }
    @Override
    public final 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) {
        InputStream in = null;
        ByteArrayOutputStream buffer = null;

        File f = new File(getTileFilename(x, y, zoom));
        if(()){
            try {
                buffer = new ByteArrayOutputStream();
                in = new FileInputStream(f);
                
                int nRead;
                byte[] data = new byte[BUFFER_SIZE];
    
                while ((nRead = (data, 0, BUFFER_SIZE)) != -1) {
                    (data, 0, nRead);
                }
                ();

                return ();
            } catch (IOException e) {
                ();
                return null;
            } catch (OutOfMemoryError e) {
                ();
                return null;
            } finally {
                if (in != null) try { (); } catch (Exception ignored) {}
                if (buffer != null) try { (); } catch (Exception ignored) {}
            }
        }else{
            return null;
        }
    }
    private String getTileFilename(int x, int y, int zoom) {
        return tilePath + zoom + '/' +'x'+ x + 'y' + y + ".png";
    }
    @Override
    public int getTileHeight() {
        
        return TILE_HEIGHT;
    }
    @Override
    public int getTileWidth() {
        
        return TILE_WIDTH;
    }

}