arcgis for android 本地缓存

    最近做的arcgis for android项目中由于移动和电信网络实在太慢,加上流量消耗也厉害。想到谷歌和百度都使用了缓存的方法。即将浏览过的地图保存到SD卡中,下次浏览相同地块的时候就不需要在从网上下载直接调用本地即可。在API中找了一通没法发现有类似功能的接口,问esri的人也没有回复。算了继承TiledServiceLayer自己实现一个吧。

     因为初始化图层的时候需要设置TileInfo的值,而这个值直接影响到地图是否可以调用成功。我在这块费大了时间了找不到原因,其实就是TileInfo的数据设置的不对,应该和你发布地图的TileInfo信息一致。这些信息可以在服务的pjson中获得 如:“http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer?f=pjson”  我写了个json解析类获得这些需要的数据

  1 /**
  2  * ====================================================
  3  * 
  4  * @Copyright (C) 2012-2013 
  5  * @All rights reserved
  6  * @filename :ReturnJson.java
  7  * @date 2013-7-29
  8  * @time 下午2:07:36
  9  * @author samxu
 10  * @description:
 11  * 
 12  * @---------------代码维护与版本信息---------------------------
 13  * @版本:V1.0 编写人:samxu 描述:第一次创建
 14  * 
 15  * 
 16  * @=====================================================
 17  */
 18 public class ReturnJson {
 19     private String strURL;
 20     private int rows; // 瓦片高度
 21     private int cols; // 瓦片宽度
 22     private int dpi; // //瓦片分辨率
 23     private Point origin; // 地图原点
 24     private int wkid; // 地图坐标ID
 25     private double[] res;        //地图分辨率
 26     private double[] scale;   //比例尺
 27     private Envelope InitEnvelope; // 初始化范围
 28     private Envelope FullEnvelope; // 最大范围
 29 
 30     public int getRows() {
 31         return rows;
 32     }
 33 
 34     public int getCols() {
 35         return cols;
 36     }
 37 
 38     public int getDpi() {
 39         return dpi;
 40     }
 41 
 42     public Point getOrigin() {
 43         return origin;
 44     }
 45 
 46     public int getWkid() {
 47         return wkid;
 48     }
 49 
 50     public double[] getRes() {
 51         return res;
 52     }
 53 
 54     public double[] getScale() {
 55         return scale;
 56     }
 57 
 58     public Envelope getInitEnvelope() {
 59         return InitEnvelope;
 60     }
 61 
 62     public Envelope getFullEnvelope() {
 63         return FullEnvelope;
 64     }
 65 
 66     public ReturnJson(String strURL) {
 67         // TODO Auto-generated constructor stub
 68         this.strURL = strURL;
 69         // String jsonString = loadAssetsString();
 70         ExplainJson();
 71     }
 72 
 73     // 解析json中关键数据保存起来
 74     public void ExplainJson() {
 75         HttpPost request = new HttpPost(strURL);
 76         try {
 77             HttpResponse httpResponse = new DefaultHttpClient()
 78                     .execute(request);
 79             String retStr = EntityUtils.toString(httpResponse.getEntity());
 80 
 81             JSONObject jsonObject_tileInfo = new JSONObject(retStr)
 82                     .getJSONObject("tileInfo");
 83             JSONObject jsonObject_initialExtent = new JSONObject(retStr)
 84                     .getJSONObject("initialExtent");
 85             JSONObject jsonObject_fullExtent = new JSONObject(retStr)
 86                     .getJSONObject("fullExtent");
 87             JSONArray jsonArray_lods = jsonObject_tileInfo.getJSONArray("lods");
 88             JSONObject jsonObject_spatialReference = jsonObject_tileInfo
 89                     .getJSONObject("spatialReference");
 90             rows = jsonObject_tileInfo.getInt("rows");
 91             cols = jsonObject_tileInfo.getInt("cols");
 92             dpi = jsonObject_tileInfo.getInt("dpi");
 93             wkid = jsonObject_spatialReference.getInt("wkid");
 94             double x = jsonObject_tileInfo.getJSONObject("origin").getDouble(
 95                     "x");
 96             double y = jsonObject_tileInfo.getJSONObject("origin").getDouble(
 97                     "y");
 98             origin = new Point(x, y);
 99             double xmin = jsonObject_initialExtent.getDouble("xmin");
100             double ymin = jsonObject_initialExtent.getDouble("ymin");
101             double xmax = jsonObject_initialExtent.getDouble("xmax");
102             double ymax = jsonObject_initialExtent.getDouble("ymax");
103             InitEnvelope = new Envelope(xmin, ymin, xmax, ymax);
104             xmin = jsonObject_fullExtent.getDouble("xmin");
105             ymin = jsonObject_fullExtent.getDouble("ymin");
106             xmax = jsonObject_fullExtent.getDouble("xmax");
107             ymax = jsonObject_fullExtent.getDouble("ymax");
108             FullEnvelope = new Envelope(xmin, ymin, xmax, ymax);
109 
110             int k = jsonArray_lods.length();
111             res = new double[k];
112             scale = new double[k];
113             for (int i = 0; i < jsonArray_lods.length(); i++) {
114 
115                 JSONObject jsonObject3 = (JSONObject) jsonArray_lods.opt(i);
116                 res[i] = jsonObject3.getDouble("resolution");
117                 scale[i] = jsonObject3.getDouble("scale");
118 
119             }
120 
121         } catch (Exception e) {
122             // TODO: handle exception
123         }
124 
125     }
126 
127 }

 

    然后要做的是继承TiledServiceLayer初始化图层;

 1 //初始化图层设置Tileinfo数据
 2     protected void initLayer() {
 3         if (getID() == 0) {
 4             this.nativeHandle = create();
 5         }
 6         try {
 7             SpatialReference localSpatialReference = SpatialReference
 8                     .create(json.getWkid());
 9             setDefaultSpatialReference(localSpatialReference);
10             Envelope FullEnvelope = json.getFullEnvelope();
11             Envelope initEnvelope = json.getInitEnvelope();
12             setFullExtent(FullEnvelope);
13             setInitialExtent(initEnvelope);
14 
15             Point localPoint = json.getOrigin();
16             double[] arrayOfDoublescale = json.getScale();
17             double[] arrayOfDoubleres = json.getRes();
18             int cols = json.getCols();
19             int dpi = json.getDpi();
20             int rows = json.getRows();
21             int k = arrayOfDoublescale.length;
22             TiledServiceLayer.TileInfo localTileInfo = new TiledServiceLayer.TileInfo(
23                     localPoint, arrayOfDoublescale, arrayOfDoubleres, k, dpi,
24                     rows, cols);
25             setTileInfo(localTileInfo);
26             super.initLayer();
27             return;
28         } catch (Exception localException) {
29             localException.printStackTrace();
30         }
31 
32     }

  

 

      重写getTile()  ,我在这里花了好长时间就是因为怎么都进不去,最后发现触发这个方法的条件是当前显示范围下有地图图块如果你没有移动到有地图的地方或者tileinfo本身就写错了那就不可能进来了

 1 //获取瓦片  如果这方法总是进不去,别想。肯定是图层初始化TileInfo数据添加的不对
 2     @Override
 3     protected byte[] getTile(int level, int col, int row) throws Exception {
 4         // TODO Auto-generated method stub
 5         byte[] bytes = null;
 6         //根据图层、行、列找本地数据
 7         bytes = getOfflineCacheFile(level, col, row);
 8         //如果本地数据为空,则调用网络数据。改接口2.0.0测试成功。最新的10.0貌似没有这个接口。具体自个找找吧,我也没有测过
 9         if (bytes == null) {
10             String strUrl = layerURL + "/tile" + "/" + level + "/" + row + "/"
11                     + col;
12             HashMap<String, String> localHashMap = new HashMap<String, String>();
13             bytes = com.esri.core.internal.b.a.a.a(strUrl, localHashMap);
14             AddOfflineCacheFile(level, col, row, bytes);
15         }
16         return bytes;
17     }

接下来就是对网络图片的保存和读取了

// 将图片保存到本地 目录结构可以随便定义 只要你找得到对应的图片
    private byte[] AddOfflineCacheFile(int level, int col, int row, byte[] bytes) {

        File file = new File(cachepath);
        if (!file.exists()) {
            file.mkdirs();
        }
        File levelfile = new File(cachepath + "/" + level);
        if (!levelfile.exists()) {
            levelfile.mkdirs();
        }
        File colfile = new File(cachepath + "/" + level + "/" + col);
        if (!colfile.exists()) {
            colfile.mkdirs();
        }
        File rowfile = new File(cachepath + "/" + level + "/" + col + "/" + row
                + ".dat");
        if (!rowfile.exists()) {
            try {
                FileOutputStream out = new FileOutputStream(rowfile);
                out.write(bytes);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return bytes;

    }

    // 从本地获取图片
    private byte[] getOfflineCacheFile(int level, int col, int row) {
        byte[] bytes = null;
        File rowfile = new File(cachepath + "/" + level + "/" + col + "/" + row
                + ".dat");

        if (rowfile.exists()) {
            try {
                bytes = CopySdcardbytes(rowfile);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            bytes = null;
        }
        return bytes;
    }

    // 读取本地图片流
    public byte[] CopySdcardbytes(File file) throws IOException {
        FileInputStream in = new FileInputStream(file);

        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);

        byte[] temp = new byte[1024];

        int size = 0;

        while ((size = in.read(temp)) != -1) {
            out.write(temp, 0, size);
        }
        in.close();
        byte[] bytes = out.toByteArray();
        return bytes;
    }

 

需要值得注意的是如果地图服务是动态出图那么就不能加载。需要源码的可以留Email ;

 

posted @ 2013-08-01 09:50  善小书  阅读(2257)  评论(34编辑  收藏  举报