-->

How to save google map in android ?and How to use

2020-08-04 10:10发布

问题:

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

回答1:

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.



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