【问题标题】:How to save google map in android ?and How to use tileprovider如何在 android 中保存谷歌地图?以及如何使用 tileprovider
【发布时间】:2015-03-23 09:24:12
【问题描述】:

我正在使用互联网创建 Android Google 地图,但我不知道如何将 Google 地图保存到 Android 应用中。
示例:Android 地图应用程序。

请帮帮我

【问题讨论】:

标签: android google-maps google-api-java-client


【解决方案1】:

你可以从不同的来源挖瓷砖。这是 TileProvider 的实现,您可以将它与 googleMap 一起使用,它可以抓取 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;
        }
    }
}

因此您可以通过 x/y 和缩放将切片缓存到数据库中。或者您可以保存特定区域。在重复使用它们之后。要为谷歌地图设置它,请使用:

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

重要提示:根据谷歌许可协议,您只能在移动设备上临时使用这种缓存。

【讨论】:

  • 请分享 SQLiteMapDatabase
【解决方案2】:

您可以尝试static maps,您可以将地图保存为图像。用户也可以自己保存离线地图:downloading offline maps

代码示例:

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

【讨论】:

  • 我尝试下载钦奈地图下载完成。但同样的过程如何做到这一点我的 android 应用程序
  • 添加了我在我的 android 应用程序中使用的代码,以显示包含地点列表中所有地点的地图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多