重写TiledServiceLayer实现本地缓存

来源:互联网 发布:北京java培训哪个好些 编辑:程序博客网 时间:2024/06/06 04:15

        有时候移动端网络不是很稳定,而且在没有Wifi情况下访问非常费流量,网速也较慢,因此实现本地缓存功能已经显得刻不容缓了。虽然现在Arcgis官方对Android还不支持这个功能,但我们完全可以自己去实现。其实思路很简单,就是在每次访问服务端的之前去判断本地是否有这个图片,如果有就读取本地的,否则去服务端获取并保存到本地。可以更形象一点:

if(本地是否有这个切片){         读取本地图片;   }else{         去服务端获取;         保存到本地;      }
对就是这么简单,下面我们看代码。

一些的参数

 private Envelope fullExtent = new Envelope(38271382.590048, 3958604.25302, 38524317.872452, 4118706.90518);//全服范围    private Envelope initExtent = new Envelope(38271382.590048, 3958604.25302, 38524317.872452, 4118706.90518);//初始范围    private TileInfo tileInfo; //TiledServiceLayer.TileInfo            private SpatialReference spatialReference;//空间参考            private String layerurl; //Url    private String cachepath;//本地存储路径
初始化Tileinfo

private void initTileInfo(){                spatialReference = SpatialReference.create(2363); //空间参考/*原点坐标*/        Point localPoint = new Point(3.28768E7,1.00021E7);        /*地图分辨率*/double[] arrayOfDoubleres = {529.16772500211675, 264.58386250105838, 132.29193125052919, 66.145965625264594, 33.072982812632297, 16.933367200067735,8.4666836000338677,4.2333418000169338, 2.1166709000084669, 1.0583354500042335, 0.52916772500211673, 0.26458386250105836};       /*地图比例尺*/       double[] arrayOfDoublescale = {2000000.0, 1000000.0, 500000.0, 250000.0, 125000.0, 64000.0, 32000.0, 16000.0,                                         8000.0, 4000.0, 2000.0, 1000.0};       int levels = arrayOfDoublescale.length;       int dpi=96;       int tileHeight = 256;//图片高度       int tileWidth = 256;//图片宽度       tileInfo = new TileInfo(localPoint, arrayOfDoublescale, arrayOfDoubleres,levels, dpi, tileWidth, tileHeight);}
重载initLayer()方法

@Overrideprotected void initLayer() {// TODO Auto-generated method stubif(getID()==0){this.nativeHandle = create();}setFullExtent(fullExtent);//设置全服地图范围setTileInfo(tileInfo);//设置tileInfosetInitialExtent(initExtent);//设置初始化地图范围setDefaultSpatialReference(spatialReference);//设置空间参考super.initLayer();}
重载geTile()方法

@Overrideprotected byte[] getTile(int level, int col, int row) throws Exception {// TODO Auto-generated method stubbyte[] bytes = getOfflineCacheFile(level,col,row);//从本地获取图片   if (bytes == null) {//如果本地没有则去服务端获取HashMap localHashMap = new HashMap();bytes = a.a(strlayer, localHashMap);AddOfflineCacheFile(level, col, row, bytes);//保存到本地    }}return bytes;}
下面是完整的代码

package com.test.common.utils;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.HashMap;import android.util.Log;import com.esri.android.map.TiledServiceLayer;import com.esri.core.geometry.Envelope;import com.esri.core.geometry.Point;import com.esri.core.geometry.SpatialReference;import com.esri.core.internal.b.a.a;/** *  * @ClassName:   OfflineCacheTiledServiceLayer * @Description: 实现本地缓存 * @author:              Zrd * @version:            v1.0   * @modify:      TODO(版本修改信息) */public class OfflineCacheTiledServiceLayer  extends TiledServiceLayer {    private SpatialReference spatialReference; //空间参考    private Envelope fullExtent = new Envelope(38271382.590048, 3958604.25302, 38524317.872452, 4118706.90518);//全服范围    private Envelope initExtent = new Envelope(38271382.590048, 3958604.25302, 38524317.872452, 4118706.90518);//初始范围    private TileInfo tileInfo; //TiledServiceLayer.TileInfo        private String layerurl; //Url    private String cachepath;//本地存储路径    public OfflineCacheTiledServiceLayer(String layerurl,String cachepath) {super(layerurl);// TODO Auto-generated constructor stubthis.layerurl = layerurl;this.cachepath = cachepath;initTileInfo();OfflineCacheTiledServiceLayer.this.initLayer();}private void initTileInfo(){spatialReference = SpatialReference.create(2363); //空间参考/*原点坐标*/        Point localPoint = new Point(3.28768E7,1.00021E7);        /*地图分辨率*/    double[] arrayOfDoubleres = {529.16772500211675, 264.58386250105838, 132.29193125052919, 66.145965625264594, 33.072982812632297, 16.933367200067735,8.4666836000338677,4.2333418000169338, 2.1166709000084669, 1.0583354500042335, 0.52916772500211673, 0.26458386250105836};    /*地图比例尺*/    double[] arrayOfDoublescale = {2000000.0, 1000000.0, 500000.0, 250000.0, 125000.0, 64000.0, 32000.0, 16000.0, 8000.0, 4000.0, 2000.0, 1000.0};int k = arrayOfDoublescale.length;int dpi=96;int tileHeight = 256;//图片高度    int tileWidth = 256;//图片宽度tileInfo = new TileInfo(localPoint, arrayOfDoublescale, arrayOfDoubleres, k, dpi, tileWidth, tileHeight);}@Overrideprotected void initLayer() {// TODO Auto-generated method stubif(getID()==0){this.nativeHandle = create();}setFullExtent(fullExtent);//设置全服地图范围setTileInfo(tileInfo);//设置tileInfosetInitialExtent(initExtent);//设置初始化地图范围setDefaultSpatialReference(spatialReference);//设置空间参考super.initLayer();}@Overridepublic TileInfo getTileInfo() {// TODO Auto-generated method stubreturn this.tileInfo;}@Overridepublic Envelope getFullExtent() {// TODO Auto-generated method stubreturn this.fullExtent;}@Overridepublic SpatialReference getSpatialReference() {// TODO Auto-generated method stubreturn this.spatialReference;}@Overrideprotected byte[] getTile(int level, int col, int row) throws Exception {// TODO Auto-generated method stubSystem.out.println("level=" + level + " col=" + col + " row=" + row);String strlayer = layerurl+"/tile/"+level+"/"+row+"/"+col;System.out.println(strlayer);Log.i("strlayer", strlayer);byte[] bytes = getOfflineCacheFile(level,col,row);    if (bytes == null) {HashMap localHashMap = new HashMap();bytes = a.a(strlayer, localHashMap);Log.e("test", bytes.toString());AddOfflineCacheFile(level, col, row, bytes);    }}return bytes;}//将图片保存到本地 目录结构可以随便定义 只要你找得到对应的图片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 blocke.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 blocke.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;      }  }





原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 西晒的墙面很烫怎么办 儿童房颜色太粉了怎么办? 小孩在家里偷钱怎么办 脾气不好的猫该怎么办 二年孩子偷钱怎么办 孩子偷钱2000报警怎么办? 我儿子十岁老是偷钱怎么办 13孩子偷同学钱怎么办 孩子偷同学的钱怎么办 儿子十四岁了老偷钱怎么办 发现初中生的儿子偷钱怎么办 被亲戚怀疑儿子偷钱怎么办 房门选的太白了怎么办 大厅地砖颜色比墙砖浅怎么办 房屋外墙渗水物业不管怎么办 走丢了怎么办教学反思 托班教案迷路了怎么办 大班安全教案遇到小偷怎么办 小班孩子舞台表演找不到位置怎么办 懂你英语学完了怎么办 小班社会走丢了怎么办 帮小老鼠搬鸡蛋怎么办 小老鼠还能怎么办鸡蛋 中班教案走丢了怎么办 走丢了怎么办可后反思 社会走丢了怎么办教案 孩子一直不吃幼儿园的饭怎么办 大班安全游泳抽筋怎么办反思 汤洒了怎么办教学反思 迷路了怎么办小班详案 大班牙又痛又摇怎么办 大班安全教案着火了怎么办 小班社会生病了怎么办反思 脚扭伤了怎么办的反思 中班安全迷了眼怎么办 15个月的小孩长泡疹怎么办 墨盒加错颜色墨水怎么办 墨盒颜色加错了怎么办 uV打印有色差是怎么办 cmyk和rgb有色差怎么办 染头发新长出来怎么办