2

I am developing an Android app. And I want a particular area map which I can add in my app and use it for offline navigation. How can it be possible?

I didn’t get a solution. I’m using the below link, but it doesn’t work.

I display a satellite map. I have put tiles files in the asset folder, but it does not work for me.

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;

    private AssetManager mAssets;

    public CustomMapTileProvider(AssetManager assets) {
        mAssets = assets;
    }

    private static final SparseArray<Rect> TILE_ZOOMS = new SparseArray<Rect>() {{
        put(8,  new Rect(135,  180,  135,  181 ));
        put(9,  new Rect(270,  361,  271,  363 ));
        put(10, new Rect(541,  723,  543,  726 ));
        put(11, new Rect(1082, 1447, 1086, 1452));
        put(12, new Rect(2165, 2894, 2172, 2905));
        put(13, new Rect(4330, 5789, 4345, 5810));
        put(14, new Rect(8661, 11578, 8691, 11621));
    }};

    private boolean hasTile(int x, int y, int zoom) {
        Rect b = TILE_ZOOMS.get(zoom);
        return b == null ? false : (b.left <= x && x <= b.right && b.top <= y && y <= b.bottom);
    }

    @Override
    public Tile getTile(int x, int y, int zoom) {
        y = fixYCoordinate(y, zoom);

        if (hasTile(x, y, zoom)) {

            byte[] image = readTileImage(x, y, zoom);
            return image == null ? null : new Tile(TILE_WIDTH, TILE_HEIGHT, image);
        }
        else {
            return NO_TILE;
        }
    }

    private int fixYCoordinate(int y, int zoom) {
        int size = 1 << zoom; // size = 2^zoom
        return size - 1 - y;
    }

    private byte[] readTileImage(int x, int y, int zoom) {
        InputStream in = null;
        ByteArrayOutputStream buffer = null;

        try {
            in = mAssets.open(getTileFilename(x, y, zoom));
            //in = mAssets.open("world_countries.mbtiles");

            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 String getTileFilename(int x, int y, int zoom) {
        return "map/" + zoom + '/' + x + '/' + y + ".png";
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    related [Offline mode for android app using google maps api](https://stackoverflow.com/q/16525822/7666442) && https://stackoverflow.com/a/25439342/7666442 && https://stackoverflow.com/a/41359719/7666442 – AskNilesh Feb 27 '19 at 06:06
  • 1
    Possible duplicate of [Open Street Map working offline android](https://stackoverflow.com/questions/14794705/open-street-map-working-offline-android) – Elio Lako Feb 27 '19 at 09:01
  • i have put download tiles in assest folder and read that file...but if i have one time online then map display in offline ...direct offline satelite map not display ...can it possible without onlline ..direct read tiles from assest folder and display offlinemap ... – bhoomika patel Feb 28 '19 at 04:30

0 Answers0