TianDiTuLocalTiledMapServiceLayer.java 5.05 KB
package com.pashanhoo.landsurvey.layers.tianditu;

import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Environment;
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.pashanhoo.landsurvey.R;
import com.pashanhoo.landsurvey.utils.AppInfo;

import java.util.concurrent.RejectedExecutionException;

/**
 * Created by jiangbotao on 2018/3/25.
 */

public class TianDiTuLocalTiledMapServiceLayer extends TiledServiceLayer {

    String sqlitedb = "";
    private TileInfo tiandituTileInfo;
    private String layername = "";

    public TianDiTuLocalTiledMapServiceLayer(String url, String layername) {
        super(url);
        this.sqlitedb = url;
        this.layername = layername;
        try {
            getServiceExecutor().submit(new Runnable() {

                public final void run() {
                    a.initLayer();
                }

                final TianDiTuLocalTiledMapServiceLayer a;


                {
                    a = TianDiTuLocalTiledMapServiceLayer.this;
                    //super();
                }
            });
            return;
        } catch (RejectedExecutionException _ex) {
        }
    }


    protected void initLayer() {
        this.buildTileInfo();
        this.setFullExtent(new Envelope(-180, -90, 180, 90));
        this.setDefaultSpatialReference(SpatialReference.create(4490));   //CGCS2000
        //this.setDefaultSpatialReference(SpatialReference.create(4326));
        this.setInitialExtent(new Envelope(90.52, 33.76, 113.59, 42.88));
        super.initLayer();
    }


    @Override
    protected byte[] getTile(int level, int col, int row) throws Exception {
        byte[] result = null;
        try{
            result = getOfflineCacheDB(level, col, row);
        }catch (Exception ex) {
            ex.printStackTrace();
        }

        return result;
    }

    private byte[] getOfflineCacheDB(int level, int col, int row) throws Exception {

        byte[] result = null;
        Log.i("local", level + ":"+ col + ":" + row);
//        row = (int) (Math.pow(2, level) - col - row);
        String sql = "select * from " + this.layername + " where TILELEVEL = " + level + " and TILECOL = " + col + " and TILEROW = " + row;

        Cursor mCursor =
                SQLiteDatabase.openOrCreateDatabase(Environment.getExternalStorageDirectory()+"/keymapinfo/" + this.sqlitedb, null)
                        .rawQuery(sql, null);
        boolean hasData = false;

        while (mCursor.moveToNext()) {//判断是否存在数据
            hasData = true;
        }
        if (hasData) {//数据库中有数据
            try {
                if (mCursor.moveToFirst()) {
                    result = mCursor.getBlob(mCursor.getColumnIndex("TILEDATA"));
                }
                mCursor.close();
            } catch (Exception e) {
                e.printStackTrace();
                result = null;
            }
        }
        return result;
    }

    private void buildTileInfo() {
        Point originalPoint = new Point(-180, 90);

        double[] res = {
                1.40625,
                0.703125,
                0.3515625,
                0.17578125,
                0.087890625,
                0.0439453125,
                0.02197265625,
                0.010986328125,
                0.0054931640625,
                0.00274658203125,
                0.001373291015625,
                0.0006866455078125,
                0.00034332275390625,
                0.000171661376953125,
                8.58306884765629E-05,
                4.29153442382814E-05,
                2.14576721191407E-05,
                1.07288360595703E-05,
                5.36441802978515E-06,
                2.68220901489258E-06,
                1.34110450744629E-06
        };
        double[] scale = {
                400000000,
                295497598.5708346,
                147748799.285417,
                73874399.6427087,
                36937199.8213544,
                18468599.9106772,
                9234299.95533859,
                4617149.97766929,
                2308574.98883465,
                1154287.49441732,
                577143.747208662,
                288571.873604331,
                144285.936802165,
                72142.9684010827,
                36071.4842005414,
                18035.7421002707,
                9017.87105013534,
                4508.93552506767,
                2254.467762533835,
                1127.2338812669175,
                563.616940
        };
        int levels = 21;
        int dpi = 96;
        int tileWidth = 256;
        int tileHeight = 256;
        this.tiandituTileInfo = new com.esri.android.map.TiledServiceLayer.TileInfo(originalPoint, scale, res, levels, dpi, tileWidth, tileHeight);
        this.setTileInfo(this.tiandituTileInfo);
    }
}