0

I am creating an Android Google map using internet, but I don't know how to save Google map in an Android app.
Example: Android Maps App.

Please, help me

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Jai
  • 486
  • 1
  • 8
  • 21

2 Answers2

3

You can grub tiles from different sources. This is implementation of TileProvider which you can use with googleMap which grab GoogleMapTiles

   public class GoogleMapOfflineTileProvider implements TileProvider
    {

        private static final String UPPER_ZOOM_TILE_URL;


        static
        {

            UPPER_ZOOM_TILE_URL = "http://mt0.google.com/vt/lyrs=m&hl=ru&x=%d&y=%d&z=%d&scale=1&s=Galileo";
        }


        private static final String TAG = GoogleMapOfflineTileProvider.class.getName();

        private SQLiteMapDatabase sqLiteMapDatabase;


        public GoogleMapOfflineTileProvider(File file)
        {
            this(file.getAbsolutePath());
        }


        public GoogleMapOfflineTileProvider(String pathToFile)
        {
            sqLiteMapDatabase = new SQLiteMapDatabase();
            sqLiteMapDatabase.setFile(new File(pathToFile));
        }


        @Override
        public Tile getTile(int x, int y, int z)
        {
            Tile tile = NO_TILE;

                if ( sqLiteMapDatabase.existsTile(x, y, z) )
                {
                    tile = new Tile(256, 256, sqLiteMapDatabase.getTile(x, y, z));
                }
                else if ( z < 11 )
                {
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    getBitmapFromURL(x, y, z).compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    tile = new Tile(256, 256, stream.toByteArray());
                }

            return tile;
        }


    public static Bitmap getBitmapFromURL(int x, int y, int z)
    {
        Log.d(TAG, String.format(UPPER_ZOOM_TILE_URL, x, y, z));
        try
        {
            URL url = new URL(String.format(UPPER_ZOOM_TILE_URL, x, y, z));
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            return BitmapFactory.decodeStream(connection.getInputStream());
        }
        catch (IOException e)
        {
            Log.d(TAG, "exception when retrieving bitmap from internet" + e.toString());
            return null;
        }
    }
}

so you can cache tiles into your database by x/y and zoom. Or you can save specific area. And after just reuse them. To set it for google maps use:

tileProvider = new GoogleMapOfflineTileProvider(file);
offlineOverlay = googleMap.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider).zIndex(3000));

IMPORTANT : you can use this kind of caching only temporarily on mobile devices according google license agreement.

ar-g
  • 3,417
  • 2
  • 28
  • 39
2

You can try static maps, you can save your map as image. Also user can save offline maps on his own: downloading offline maps.

Code Example:

public Bitmap getBitmap() {
    Bitmap bmp = null;
    try {
        List<LatLng> places = getPlaces();
        String mapUrl = "https://maps.googleapis.com/maps/api/staticmap?size=600x600&maptype=roadmap"; 
        for (LatLng place : places) {
            mapUrl += "&markers=" + place.latitude + "," + place.longitude;
        }

        InputStream in = new URL(mapUrl).openStream();
        bmp = BitmapFactory.decodeStream(in);
    } catch (Exception ex) {
        // log error
    }
    return bmp;
}
Maksim
  • 264
  • 7
  • 20
  • i try to download chennai map download completed.but same porocess how to do this my android app – Jai Mar 23 '15 at 10:28
  • Added code that i used in my android app to show the map with all places in places list. – Maksim Mar 23 '15 at 12:58