Java – Android Google Maps V2 – SD card as tile provider
                                        
                    •
                    Java                                    
                I am using Google Maps API V2 to develop an Android application. I have to use offline tiles. I have all tiles of the whole city in my SD card (from PNG format open street map)
Solution
I modified something and it worked This is the code:
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 zoom) {
        FileInputStream in = null;
        ByteArrayOutputStream buffer = null;
        try { in = new FileInputStream(getTileFile(x,zoom));
            buffer = new ByteArrayOutputStream();
            int nRead;
            byte[] data = new byte[BUFFER_SIZE];
            while ((nRead = in .read(data,BUFFER_SIZE)) != -1) {
                buffer.write(data,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 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 Google map instance
... map.setMapType(GoogleMap.MAP_TYPE_NONE); TileOverlayOptions tileOverlay = new TileOverlayOptions(); tileOverlay.tileProvider(new CustomMapTileProvider()); map.addTileOverlay(tileOverlay).setZIndex(0); ...
                            The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
                        
                                            
                        THE END
                    
                    
                    
                                                        二维码
                        
                        
                                                
                        