1. 程式人生 > >【worldwind學習】worldwind android版新增天地圖切片圖層

【worldwind學習】worldwind android版新增天地圖切片圖層

在國內還是天地圖速度快又清晰,google中國版的有偏移,國外版的被封鎖,還是天地圖首選!

/*
 * Copyright (c) 2017 United States Government as represented by the Administrator of the
 * National Aeronautics and Space Administration. All Rights Reserved.
 */

package gov.nasa.worldwind.layer;

import gov.nasa.worldwind.WorldWind;
import gov.nasa.worldwind.geom.Sector;
import gov.nasa.worldwind.render.ImageOptions;
import gov.nasa.worldwind.render.ImageSource;
import gov.nasa.worldwind.render.ImageTile;
import gov.nasa.worldwind.shape.TiledSurfaceImage;
import gov.nasa.worldwind.util.Level;
import gov.nasa.worldwind.util.LevelSet;
import gov.nasa.worldwind.util.LevelSetConfig;
import gov.nasa.worldwind.util.Logger;
import gov.nasa.worldwind.util.Tile;
import gov.nasa.worldwind.util.TileFactory;

/**
 * Created by zmy on 2017/7/15.
 */

public class TiandituLayer extends RenderableLayer implements TileFactory {

    //protected TileFactory tiandituTileFactory;

    String urlAddress = "";

    public TiandituLayer()
    {
        this("http://t0.tianditu.com/img_c/wmts");
    }

    public TiandituLayer(String serviceAddress)
    {
        if (serviceAddress == null) {
            throw new IllegalArgumentException(
                Logger.logMessage(Logger.ERROR, "BlueMarbleLandsatLayer", "constructor", "missingServiceAddress"));
        }

        urlAddress = serviceAddress;
        //tiandituTileFactory = new WmtsTileFactory();

        // Configure this layer's level set to capture the entire globe at 15m resolution.
        double metersPerPixel = 15;
        double radiansPerPixel = metersPerPixel / WorldWind.WGS84_SEMI_MAJOR_AXIS;
        LevelSetConfig levelsConfig = new LevelSetConfig(null, 45, 16, 256, 256);
        //levelsConfig.numLevels = levelsConfig.numLevelsForResolution(radiansPerPixel);

        this.setDisplayName("TiandituSat");
        this.setPickEnabled(false);

        TiledSurfaceImage surfaceImage = new TiledSurfaceImage();
        surfaceImage.setLevelSet(new LevelSet(levelsConfig));
        surfaceImage.setTileFactory(this);
        surfaceImage.setImageOptions(new ImageOptions(WorldWind.RGB_565)); // reduce memory usage by using a 16-bit configuration with no alpha
        this.addRenderable(surfaceImage);

    }

    @Override
    public Tile createTile(Sector sector, Level level, int row, int column) {
        ImageTile tile = new ImageTile(sector, level, row, column);

        //String urlString = urlAddress;//this.urlForTile(level.levelNumber, row, column);

        int row1 = (int) Math.pow(2, (level.levelNumber + 2)) - 1 - row;//計算行列和級數
        int col1 = column;
        int level1 = level.levelNumber+3;

        String serverURL = urlAddress.replaceFirst("0", String.valueOf((int)(Math.random() * 8)));//由於伺服器端採用了叢集技術,http://tile0/同http://tile7/取的是同一圖片
        //瓦片URL串
        String urlString = serverURL + "?request=GetTile&service=wmts&version=1.0.0&serviceMode=kvp&layer=img&Style=default&Format=tiles&TileMatrixSet=c&TileMatrix="+level1+"&TileRow="+row1+"&TileCol=" + col1;

        if (urlString != null) {
            tile.setImageSource(ImageSource.fromUrl(urlString));
        }

        return tile;
    }
}