天天看點

ArcGIS Runtime SDK for Android100.5.0端加載天地圖服務建構TianDiTuLayer類建構TianDiTuLayer對象前端加載

天地圖服務加載

  • 建構TianDiTuLayer類
  • 建構TianDiTuLayer對象
  • 前端加載

我們知道,自2019年1月起,天地圖服務的調用都需要擷取授權。在這種模式下,之前通過WebTiledLayer加載的方式在Android端就無法顯示了,由下圖可知,雖然正确發送了切片請求,但是,Runtime Android端傳回的Content-Type是text/html,而不是能夠解析的image/png。

![Web

ArcGIS Runtime SDK for Android100.5.0端加載天地圖服務建構TianDiTuLayer類建構TianDiTuLayer對象前端加載

基于此,我們将通過繼承ImageTiledLayer并擴充protected abstract byte[] getTile​(TileKey tileKey)方法直接傳回獲得的圖檔位元組流,進而建構天地圖類,進而實作天地圖的加載。

閑話少叙,直接上代碼片段。

建構TianDiTuLayer類

public class TianDiTuLayer extends ImageTiledLayer {
    private String preTemplateURL;
    private Envelope fullExtent;
    private TileInfo tileInfo;

    protected TianDiTuLayer(String preTemplateURL, TileInfo tileInfo, Envelope fullExtent) {
        super(tileInfo, fullExtent);
        this.preTemplateURL = preTemplateURL;
    }

    @Override
    protected byte[] getTile(TileKey tileKey) {
        int level = tileKey.getLevel();
        int column = tileKey.getColumn();
        int row = tileKey.getRow();

        String tileURL = preTemplateURL + "&TileMatrix=" + level + "&TileCol=" + column + "&TileRow=" + row;
        // Log.i("TemplateURL", "The URL is:" + tileURL);
       
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            URL url = new URL(tileURL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5 * 1000);
            InputStream inStream = conn.getInputStream();
            byte[] btImg= readInputStream(inStream);
            return btImg;

        }catch(Exception e){
            e.printStackTrace();
        }

        return null;
    }

    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        inStream.close();
        outStream.flush();
        return outStream.toByteArray();
     }
}
           

建構TianDiTuLayer對象

ArrayList<LevelOfDetail> levels = new ArrayList<LevelOfDetail>();
        levels.add(new LevelOfDetail(1,0.703125,295497593.05875003));
        levels.add(new LevelOfDetail(2,0.3515625,147748796.52937502));
        levels.add(new LevelOfDetail(3, 0.17578125, 73874398.264687508));
        levels.add(new LevelOfDetail(4, 0.087890625, 36937199.132343754));
        levels.add(new LevelOfDetail(5, 0.0439453125, 18468599.566171877));
        levels.add(new LevelOfDetail(6, 0.02197265625, 9234299.7830859385));
        levels.add(new LevelOfDetail(7, 0.010986328125, 4617149.8915429693));
        levels.add(new LevelOfDetail(8, 0.0054931640625, 2308574.9457714846));
        levels.add(new LevelOfDetail(8, 0.00274658203125, 1154287.4728857423));
        levels.add(new LevelOfDetail(10, 0.001373291015625, 577143.73644287116));
        levels.add(new LevelOfDetail(11, 0.0006866455078125, 288571.86822143558));
        levels.add(new LevelOfDetail(12, 0.00034332275390625, 144285.93411071779));
        levels.add(new LevelOfDetail(13, 0.000171661376953125, 72142.967055358895));
        levels.add(new LevelOfDetail(14, 8.58306884765625e-005, 36071.483527679447));
        levels.add(new LevelOfDetail(15, 4.291534423828125e-005, 18035.741763839724));
        levels.add(new LevelOfDetail(16, 2.1457672119140625e-005, 9017.8708819198619));
        levels.add(new LevelOfDetail(17, 1.0728836059570313e-005, 4508.9354409599309));
        levels.add(new LevelOfDetail(18, 5.3644180297851563e-006, 2254.4677204799655));
        levels.add(new LevelOfDetail(19, 2.6822090148925781e-006, 1127.2338602399827));
        levels.add(new LevelOfDetail(20, 1.3411045074462891e-006, 563.61693011999137));

        int iDPI = 96;
        int iTileWidth = 256;
        int iTileHeight = 256;

        tileInfo = new TileInfo(iDPI, TileInfo.ImageFormat.MIXED, levels, new Point(-180, 90, SpatialReference.create(4490)), SpatialReference.create(4490), iTileHeight, iTileWidth);
        fullExtent = new Envelope(-180.0,-90.0,180.0,90.0, SpatialReference.create(4490));
        String preTemplateURL = "http://t0.tianditu.com/vec_c/wmts?tk=6dfd31e3b55a8466f34997aee5551a9c&service=wmts&request=GetTile&version=1.0.0&LAYER=vec&tileMatrixSet=c&&style=default&format=tiles";
        TianDiTuLayer tianDiTuLayer = new TianDiTuLayer(preTemplateURL, tileInfo, fullExtent);
           

前端加載

arcGISMap.getBasemap().getBaseLayers().add(tianDiTuLayer);
           
ArcGIS Runtime SDK for Android100.5.0端加載天地圖服務建構TianDiTuLayer類建構TianDiTuLayer對象前端加載

由于本文主要目的是測試邏輯是否可行,代碼細節并未過多考慮。請自行優化。

繼續閱讀