基本信息
源码名称:Android的关于高德地图加载谷歌瓦片,并缓存本地的功能.
源码大小:25.87M
文件格式:.rar
开发语言:Java
更新时间:2021-05-28
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


最近开发的Android的App需要使用到卫星图,不过发现国内现有的百度,高德的卫星图对郊区图层支持的不好,只能使用谷歌的卫星图.

在尝试使用谷歌的Google Map API for Android后又发现去要手机安装谷歌服务,并且有可能用到科学上网.所以就想有没有一个既能加载谷歌卫星图的,又不需要安装谷歌服务.

最后找到了高德地图用于加载谷歌的卫星图,并且在第一次加载的时候,自动缓存本地,既能在二次加载减少加载时间,又能节省流量,毕竟是手机使用.流量能省则省



/**
     * 加载在线瓦片数据
     */
    private void useOMCMap() {
        final String url =  "http://www.google.cn/maps/vt?lyrs=y&gl=cn&x=%d&s=&y=%d&z=%d";
        //final String url =  "http://mt1.google.cn/vt/lyrs=y&hl=zh-CN&gl=cn&x=%d&s=&y=%d&z=%d";

        TileOverlayOptions tileOverlayOptions =
                new TileOverlayOptions().tileProvider(new UrlTileProvider(256, 256) {
                    @Override
                    public URL getTileUrl(int x, int y, int zoom) {
                        try {
                            //return new URL(String.format(url, zoom   1, TileXYToQuadKey(x, y, zoom)));
                            //return new URL(String.format(url, x, y, zoom));
                            String mFileDirName;
                            String mFileName;
                            mFileDirName = String.format("L%02d/", zoom   1);
                            mFileName = String.format("%s", TileXYToQuadKey(x, y, zoom));//为了不在手机的图片中显示,下载的图片取消jpg后缀,文件名自己定义,写入和读取一致即可,由于有自己的bingmap图源服务,所以此处我用的bingmap的文件名
                            String LJ = ALBUM_PATH  mFileDirName  mFileName;
                            if (MapImageCache.getInstance().isBitmapExit( mFileDirName   mFileName)) {//判断本地是否有图片文件,如果有返回本地url,如果没有,缓存到本地并返回googleurl
                                return new URL("file://"   LJ);
                            } else {
                                String filePath = String.format(url, x, y, zoom);
                                Bitmap mBitmap;
                                //mBitmap = BitmapFactory.decodeStream(getImageStream(filePath));//不知什么原因导致有大量的图片存在坏图,所以重写InputStream写到byte数组方法
                                mBitmap = getImageBitmap(getImageStream(filePath));
                                try {
                                    saveFile(mBitmap, mFileName, mFileDirName);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                return new URL(filePath);
                            }
                        }catch (Exception e) {
                            e.printStackTrace();
                        }
                        return null;
                    }
                });
        tileOverlayOptions.diskCacheEnabled(false)   //由于高德自带的瓦片缓存在关闭程序后会自动清空,所以无意义,关闭本地缓存
                .diskCacheDir("/storage/emulated/0/amap/OMCcache")
                .diskCacheSize(1024000)
                .memoryCacheEnabled(true)
                .memCacheSize(102400)
                .zIndex(-9999);
        mtileOverlay = aMap.addTileOverlay(tileOverlayOptions);
    }

瓦片数据下载途中发现会有图片出现黑块,格式损坏的问题,参考了下面进行解决

public Bitmap getImageBitmap(InputStream imputStream){
        // 将所有InputStream写到byte数组当中
        byte[] targetData = null;
        byte[] bytePart = new byte[4096];
        while (true) {
            try {
                int readLength = imputStream.read(bytePart);
                if (readLength == -1) {
                    break;
                } else {
                    byte[] temp = new byte[readLength (targetData == null ? 0 : targetData.length)];
                    if (targetData != null) {
                        System.arraycopy(targetData, 0, temp, 0, targetData.length);
                        System.arraycopy(bytePart, 0, temp, targetData.length, readLength);
                    } else {
                        System.arraycopy(bytePart, 0, temp, 0, readLength);
                    }
                    targetData = temp;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 指使Bitmap通过byte数组获取数据
        Bitmap bitmap = BitmapFactory.decodeByteArray(targetData, 0, targetData.length);
        return bitmap;
    }