0

I wanna put an image of my city which contains roads and homes on google map using groundoverlay method in android, where my image size and dimensions are very large . How I can put this image on map in android?

Ashish Choudhary
  • 444
  • 1
  • 4
  • 11

1 Answers1

0

Split your image into 256x256 pixel tiles with e.g MapTiler put it in assets folder and use tiles like in this answer of Alex Vasilkov:

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

    @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) {
        InputStream in = null;
        ByteArrayOutputStream buffer = null;

        try {
            in = mAssets.open(getTileFilename(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 String getTileFilename(int x, int y, int zoom) {
        return "map/" + zoom + '/' + x + '/' + y + ".png";
    }
}
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
  • I have only starting and ending lat longs of city then How I will set each tile above city? – Ashish Choudhary Jan 12 '19 at 13:43
  • @AshishChoudhary When you create tiles, each tile got name and path into root folder depending on it's [coordinates](https://developers.google.com/maps/documentation/javascript/coordinates) and zoom level. `TileProvider` converts lat/lng into tile names. – Andrii Omelchenko Jan 14 '19 at 08:08