Arcgis 添加谷歌地圖服務為地圖并實現(xiàn)緩存

在 gis 中,除了各種圖層服務,就是底圖了。這里記錄一下使用 谷歌地圖 作為底圖并緩存到本地的方法。

上代碼:
1、首先是自定義的地圖服務 GoogleMapLayer

public class GoogleMapLayer extends TiledServiceLayer {

    private int GoogleMapLayerType;

    public GoogleMapLayer(int layerType) {
        super(true);
        this.GoogleMapLayerType = layerType;
        this.init();
    }
    // 這里最大層級19,可以自定義(需要在下方的 scales 和 resolutions 中設置相對應的數(shù)據(jù))
    private int minLevel = 0;
    private int maxLevel = 19;

    private double[] scales = new double[] { 591657527.591555,
            295828763.79577702, 147914381.89788899, 73957190.948944002,
            36978595.474472001, 18489297.737236001, 9244648.8686180003,
            4622324.4343090001, 2311162.217155, 1155581.108577, 577790.554289,
            288895.277144, 144447.638572, 72223.819286, 36111.909643,
            18055.954822, 9027.9774109999998, 4513.9887049999998, 2256.994353,
            1128.4971760000001 };
    private double[] resolutions = new double[] { 156543.03392800014,
            78271.516963999937, 39135.758482000092, 19567.879240999919,
            9783.9396204999593, 4891.9698102499797, 2445.9849051249898,
            1222.9924525624949, 611.49622628138, 305.748113140558,
            152.874056570411, 76.4370282850732, 38.2185141425366,
            19.1092570712683, 9.55462853563415, 4.7773142679493699,
            2.3886571339746849, 1.1943285668550503, 0.59716428355981721,
            0.29858214164761665 };

    private Point origin = new Point(-20037508.342787, 20037508.342787);

    private int dpi = 96;

    private int tileWidth = 256;
    private int tileHeight = 256;

    private void init() {
        try {
            getServiceExecutor().submit(new Runnable() {
                public void run() {
                    GoogleMapLayer.this.initLayer();
                }
            });
        } catch (RejectedExecutionException rejectedexecutionexception) {
            Log.e("Google Map Layer", "initialization of the layer failed.",
                    rejectedexecutionexception);
        }
    }

    protected byte[] getTile(int level, int col, int row) throws Exception {
        if (level > maxLevel || level < minLevel) {
            return new byte[0];
        }

        String s = "Galileo".substring(0, ((3 * col + row) % 8));
        String url = "";

        switch (GoogleMapLayerType) {
            case  GoogleMapLayerTypes.IMAGE_GOOGLE_MAP:
                url = "http://mt" + (col % 4) + ".google.cn/vt/lyrs=s&hl=zh-CN&gl=cn&" +
                        "x=" + col + "&" +
                        "y=" + row + "&" +
                        "z=" + level + "&" +
                        "s=" + s;
                break;
            case GoogleMapLayerTypes.VECTOR_GOOGLE_MAP:
                url = "http://mt2.google.cn/vt/lyrs=m@158000000&hl=zh-CN&gl=cn&" +
                        "x=" + col + "&" +
                        "y=" + row + "&" +
                        "z=" + level + "&" +
                        "s=" + s;
                break;
            case GoogleMapLayerTypes.TERRAIN_GOOGLE_MAP:
                url = "http://mt" + (col % 4) + ".google.cn/vt/lyrs=t@131,r@227000000&hl=zh-CN&gl=cn&" +
                        "x=" + col + "&" +
                        "y=" + row + "&" +
                        "z=" + level + "&" +
                        "s=" + s;
                break;
            case GoogleMapLayerTypes.ANNOTATION_GOOGLE_MAP:
                url = "http://mt" + (col % 4) + ".google.cn/vt/imgtp=png32&lyrs=h@169000000&hl=zh-CN&gl=cn&" +
                        "x=" + col + "&" +
                        "y=" + row + "&" +
                        "z=" + level + "&" +
                        "s=" + s;
                break;
        }
        // 這里是設置緩存到本地的路徑 可以自定義
        String imgPtPath=MainActivity.defaultPath+"t"+GoogleMapLayerType+"/";
        File dirFile = new File(imgPtPath);
        if(!dirFile.exists()){
            dirFile.mkdirs();
        }
        String imgPath=imgPtPath+"l"+level+"/";
        String imgName ="c"+col+"r"+row+".png";
        if(MainActivity.isConnect()){
            Map<String, String> map = null;
            byte[] btImg= com.esri.core.internal.io.handler.a.a(url, map);
            saveFile(imgPath,btImg,imgName);
            return btImg;
        }else {
            Bitmap bitmap = BitmapFactory.decodeFile(imgPath+imgName);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            return baos.toByteArray();
        }
    }

    public void saveFile(String filePath,byte[] data, String fileName) throws IOException {
        Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);// bitmap
        File dirFile = new File(filePath);
        if(!dirFile.exists()){
            dirFile.mkdir();
        }
        // 檢測是否有 .nomedia 文件,該文件防止相冊等媒體軟件掃描離線地圖圖片,以免造成不必要的麻煩
        File nomediaFile = new File(filePath+".nomedia");
        if (!nomediaFile.exists()){
            nomediaFile.createNewFile();
        }
        File myCaptureFile = new File(filePath + fileName);
        // 判斷離線的圖片是否已經存在,已經存在的地圖不用再次下載(節(jié)省流量)
        if (myCaptureFile.exists()){
            return;
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        mBitmap.compress(Bitmap.CompressFormat.PNG, 80, bos);
        bos.flush();
        bos.close();
    }

    protected SpatialReference getSptialReference(){
        return this.getDefaultSpatialReference();
    }
    protected void initLayer() {
        if (getID() == 0L) {
            nativeHandle = create();
            changeStatus(com.esri.android.map.event.OnStatusChangedListener.STATUS
                    .fromInt(-1000));
        } else {
            this.setDefaultSpatialReference(SpatialReference.create(102113));
            this.setFullExtent(new Envelope(-22041257.773878,
                    -32673939.6727517, 22041257.773878, 20851350.0432886));
            this.setTileInfo(new TileInfo(origin, scales, resolutions,
                    scales.length, dpi, tileWidth, tileHeight));
            super.initLayer();
        }
    }
}

2、接著是谷歌地圖的類型

public interface GoogleMapLayerTypes {

    /**
     * 矢量圖
     */
    final int VECTOR_GOOGLE_MAP = 1;
    /**
     * 影像圖
     */
    final int IMAGE_GOOGLE_MAP = 2;
    /**
     * 地形圖
     */
    final int TERRAIN_GOOGLE_MAP = 3;
    /**
     * 標注
     */
    final int ANNOTATION_GOOGLE_MAP = 4;
}

在代碼中使用,很簡單:獲取地圖服務實例并添加到mapview中就行

GoogleMapLayer googleLayer = new GoogleMapLayer(GoogleMapLayerTypes.VECTOR_GOOGLE_MAP);
mMapView.addLayer(googleLayer);

僅供大家學習、參考。
參考文章:http://blog.csdn.net/u014014578/article/details/21476395

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容