Android WorldWind的使用与添加天地图影像

前面讲到在Worldwind上加载Geoserver影像,现在记录下载worldwind上加载天地图。
要加载天地图影像,首先需要在天地图官网申请KEY值
进入官网,创建应用
在这里插入图片描述
填写SHA1
在这里插入图片描述
带有星号的是必填项。获取SHA1值的方法就不在赘述了。填写好后点击提交,就会生成key值。
进入到开发程序中。

public class TiandituLayers extends RenderableLayer implements TileFactory {

    String urlAddress = "";

    public static final int TIANDITU_IMAGE_LAYER = 1;
    public static final int TIANDITU_IMAGE_ANNOTATION_LAYER = 2;
    public static final int TIANDITU_VECTOR_LAYER = 3;
    public static final int TIANDITU_VECTOR_ANNOTATION_LAYER = 4;

    //天地图影像图层地址
    private static final String IMAGE_URL = "http://t0.tianditu.gov.cn/img_c/wmts";

    //天地图影像图层注记地址
    private static final String IMAGE_ANNOTATION_URL = "http://t0.tianditu.gov.cn/cia_c/wmts";

    //天地图矢量图层地址
    private static final String VECTOR_URL = "http://t0.tianditu.gov.cn/vec_c/wmts";

    //天地图矢量图层注记地址
    private static final String VECTOR_ANNOTATION_URL = "http://t0.tianditu.gov.cn/cva_c/wmts";

    //天地图申请的key:发现自2019年1月1日起地图API及服务接口调用都需要获得开发授权,所以就需要在请求ip地址之后加上申请到的key值
    private static final String TIANDITU_KEY = "申请到的Key值";

    public TiandituLayers(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/取的是同一图片
        String urlString = "";

        if (urlAddress.contains("img_c")) {
            urlString = getLayerUrl(serverURL, "img", level1, row1, col1);
        } else if (urlAddress.contains("vec_c")){
            urlString = getLayerUrl(serverURL, "vec", level1, row1, col1);
        } else if (urlAddress.contains("cia_")) {
            urlString = getLayerUrl(serverURL, "cia", level1, row1, col1);
        } else if (urlAddress.contains("cva_c")) {
            urlString = getLayerUrl(serverURL, "cva", level1, row1, col1);
        }

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

    //定义url
    private String getLayerUrl(String serverURL, String layerPattern, int level, int row, int col) {
        return serverURL + "?service=wmts" +
                "&request=gettile" +
                "&version=1.0.0" +
                "&layer=" + layerPattern +
                "&Style=default" +
                "&Format=tiles" +
                "&TileMatrixSet=c" +
                "&TileMatrix=" + level +
                "&TileRow=" + row +
                "&TileCol=" + col +
                "&tk=" + TIANDITU_KEY;
    }
}

之后在创建worldwind的窗口(createWorldWindow())方法中添加应用即可。

 public WorldWindow createWorldWindow() {
        worldWindow = new WorldWindow(getApplicationContext());
        worldWindow.getLayers().addLayer(new BackgroundLayer());
        worldWindow.getGlobe().getElevationModel().addCoverage(new BasicElevationCoverage());
        //去掉了suspension图层
        //worldWindow.getLayers().addLayer(new BlueMarbleLandsatLayer());
        //定义进来虚拟地球界面的观测点
        worldWindow.getNavigator().setLatitude(39.5960);
        worldWindow.getNavigator().setLongitude(116.7719);
        worldWindow.getNavigator().setAltitude(0.2e8);
        //addBasicLayer();//添加的Geoserver图层

        tiandituLayer = new TiandituLayers("http://t0.tianditu.gov.cn/img_c/wmts");//添加天地图的影像
        tiandituLayer2 = new TiandituLayers("http://t0.tianditu.gov.cn/cia_c/wmts");//添加天地图的矢量图层
        worldWindow.getLayers().addLayer(tiandituLayer);
        worldWindow.getLayers().addLayer(tiandituLayer2);

        locateLayer = new RenderableLayer("VectorTest");
        worldWindow.getLayers().addLayer(locateLayer);
        return worldWindow;
    }

注意如果加载时出现418代码,就需要注意下获取到的key值是否正确。自2019年1月1日起地图API及服务接口调用都需要获得开发授权,所以就需要在请求ip地址之后加上申请到的key值。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nanjumufeng

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值