Arcgis runtime for Android 100.5 加载天地图

加载高德地图

说明一下,什么时候加载高德地图,什么时候加载天地图

使用原生定位或者使用arcgis提供的LocationDisplay的定位时,需要加载天地图,因为原生定位返回的坐标是wgs84的坐标,而高德地图是火星坐标系(gcj02)是经过偏移的,定位显示会有偏移
使用百度定位或者高德定位,定位返回坐标设置为gcj02时,直接加载高德地图,定位就不会有偏移


废话不多说,上才艺

准备

先自定义TianDiTuTiledLayerClass

import com.esri.arcgisruntime.arcgisservices.LevelOfDetail;
import com.esri.arcgisruntime.arcgisservices.TileInfo;
import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.layers.WebTiledLayer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 天地图
 */

public class TianDiTuTiledLayerClass {
    //需要自己去申请,绑定包名和SHA1,我这个你们用不了
    private static final String key = "11fc0ecfe024734754f0fb5952aa5c6f";
    private static final List SubDomain = 
                         Arrays.asList(new String[]{"t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"});

    private static final int DPI = 96;
    private static final int minZoomLevel = 1;
    private static final int maxZoomLevel = 18;
    private static final int tileWidth = 256;
    private static final int tileHeight = 256;
    private static final SpatialReference SRID_MERCATOR = SpatialReference.create(102100);

    private static final double X_MIN_MERCATOR = -20037508.3427892;
    private static final double Y_MIN_MERCATOR = -20037508.3427892;
    private static final double X_MAX_MERCATOR = 20037508.3427892;
    private static final double Y_MAX_MERCATOR = 20037508.3427892;
    private static final Point ORIGIN_MERCATOR = 
                         new Point(-20037508.3427892, 20037508.3427892, SRID_MERCATOR);
    private static final Envelope ENVELOPE_MERCATOR =
                                   new Envelope(X_MIN_MERCATOR,
                                                Y_MIN_MERCATOR, 
                                                X_MAX_MERCATOR, 
                                                Y_MAX_MERCATOR, 
                                                SRID_MERCATOR);

    private static final SpatialReference SRID_2000 = SpatialReference.create(4490);
    private static final double X_MIN_2000 = -180;
    private static final double Y_MIN_2000 = -90;
    private static final double X_MAX_2000 = 180;
    private static final double Y_MAX_2000 = 90;
    private static final Point ORIGIN_2000 = new Point(-180, 90, SRID_2000);
    private static final Envelope ENVELOPE_2000 = 
                         new Envelope(X_MIN_2000, 
                                               Y_MIN_2000, 
                                               X_MAX_2000, 
                                               Y_MAX_2000, 
                                               SRID_2000);

    private static final double[] SCALES = {
            2.958293554545656E8, 1.479146777272828E8,
            7.39573388636414E7, 3.69786694318207E7,
            1.848933471591035E7, 9244667.357955175,
            4622333.678977588, 2311166.839488794,
            1155583.419744397, 577791.7098721985,
            288895.85493609926, 144447.92746804963,
            72223.96373402482, 36111.98186701241,
            18055.990933506204, 9027.995466753102,
            4513.997733376551, 2256.998866688275,
            1128.4994333441375
    };
    private static final double[] RESOLUTIONS_MERCATOR = {
            78271.51696402048, 39135.75848201024,
            19567.87924100512, 9783.93962050256,
            4891.96981025128, 2445.98490512564,
            1222.99245256282, 611.49622628141,
            305.748113140705, 152.8740565703525,
            76.43702828517625, 38.21851414258813,
            19.109257071294063, 9.554628535647032,
            4.777314267823516, 2.388657133911758,
            1.194328566955879, 0.5971642834779395,
            0.298582141738970};
    private static final double[] RESOLUTIONS_2000 = {
            0.7031249999891485, 0.35156249999999994,
            0.17578124999999997, 0.08789062500000014,
            0.04394531250000007, 0.021972656250000007,
            0.01098632812500002, 0.00549316406250001,
            0.0027465820312500017, 0.0013732910156250009,
            0.000686645507812499, 0.0003433227539062495,
            0.00017166137695312503, 0.00008583068847656251,
            0.000042915344238281406, 0.000021457672119140645,
            0.000010728836059570307, 0.000005364418029785169};

    public static WebTiledLayer CreateTianDiTuTiledLayer(LayerType layerType) {
        String type = "";
        String tilematrixset = "";
        WebTiledLayer webTiledLayer = null;
        String mainUrl;
        TileInfo mainTileInfo;
        Envelope mainEnvelope;
        try {
            switch (layerType) {
                case TIANDITU_VECTOR_MERCATOR:
                    type = "vec";
                    tilematrixset = "w";
                    break;
                case TIANDITU_VECTOR_MERCATOR_LABLE:
                    type = "cva";
                    tilematrixset = "w";
                    break;
                case TIANDITU_VECTOR_2000:
                    type = "vec";
                    tilematrixset = "c";
                    break;
                case TIANDITU_VECTOR_2000_LABLE:
                    type = "cva";
                    tilematrixset = "c";
                    break;
                case TIANDITU_IMAGE_MERCATOR:
                    type = "img";
                    tilematrixset = "w";
                    break;
                case TIANDITU_IMAGE_MERCATOR_LABLE:
                    type = "cia";
                    tilematrixset = "w";
                    break;
                case TIANDITU_IMAGE_2000:
                    type = "img";
                    tilematrixset = "c";
                    break;
                case TIANDITU_IMAGE_2000_LABLE:
                    type = "cia";
                    tilematrixset = "c";
                    break;
                case TIANDITU_TERRAIN_MERCATOR:
                    type = "ter";
                    tilematrixset = "w";
                    break;
                case TIANDITU_TERRAIN_MERCATOR_LABLE:
                    type = "cta";
                    tilematrixset = "w";
                    break;
                case TIANDITU_TERRAIN_2000:
                    type = "ter";
                    tilematrixset = "c";
                    break;
                case TIANDITU_TERRAIN_2000_LABLE:
                    type = "cta";
                    tilematrixset = "c";
                    break;
            }
            mainUrl = "http://t0.tianditu.gov.cn/"
                    + type + "_" + tilematrixset + "/wmts?" +
                    "service=wmts" +
                    "&request=gettile" +
                    "&version=1.0.0" +
                    "&layer=" + type +
                    "&format=tiles" +
                    "&STYLE=default" +
                    "&tilematrixset=" + tilematrixset +
                    "&tilecol={col}" +
                    "&tilerow={row}" +
                    "&tilematrix={level}" +
                    "&tk=" + key;
            List mainLevelOfDetail = new ArrayList();
            Point mainOrigin;
            if (tilematrixset.equals("c")) {
                for (int i = minZoomLevel; i <= maxZoomLevel; i++) {
                    LevelOfDetail item = 
                      new LevelOfDetail(i, RESOLUTIONS_2000[i - 1], SCALES[i - 1]);
                    mainLevelOfDetail.add(item);
                }
                mainEnvelope = ENVELOPE_2000;
                mainOrigin = ORIGIN_2000;
            } else {
                for (int i = minZoomLevel; i <= maxZoomLevel; i++) {
                    LevelOfDetail item = 
                      new LevelOfDetail(i, RESOLUTIONS_MERCATOR[i - 1], SCALES[i - 1]);
                    mainLevelOfDetail.add(item);
                }
                mainEnvelope = ENVELOPE_MERCATOR;
                mainOrigin = ORIGIN_MERCATOR;
            }
            mainTileInfo = new TileInfo(
                    DPI,
                    TileInfo.ImageFormat.PNG24,
                    mainLevelOfDetail,
                    mainOrigin,
                    mainOrigin.getSpatialReference(),
                    tileHeight,
                    tileWidth
            );
            webTiledLayer = new WebTiledLayer(
                    mainUrl,
                    SubDomain,
                    mainTileInfo,
                    mainEnvelope);
            webTiledLayer.setName(type);
            webTiledLayer.loadAsync();
        } catch (Exception e) {
            e.getCause();
        }
        return webTiledLayer;
    }

    public enum LayerType {
        /**
         * 天地图矢量墨卡托投影地图服务
         */
        TIANDITU_VECTOR_MERCATOR,
        TIANDITU_VECTOR_MERCATOR_LABLE,
        /**
         * 天地图矢量2000地图服务
         */
        TIANDITU_VECTOR_2000,
        TIANDITU_VECTOR_2000_LABLE,

        /**
         * 天地图影像墨卡托地图服务
         */
        TIANDITU_IMAGE_MERCATOR,
        TIANDITU_IMAGE_MERCATOR_LABLE,

        /**
         * 天地图影像2000地图服务
         */
        TIANDITU_IMAGE_2000,
        TIANDITU_IMAGE_2000_LABLE,
        /**
         * 天地图地形墨卡托地图服务
         */
        TIANDITU_TERRAIN_MERCATOR,
        TIANDITU_TERRAIN_MERCATOR_LABLE,
        /**
         * 天地图地形2000地图服务
         */
        TIANDITU_TERRAIN_2000,
        TIANDITU_TERRAIN_2000_LABLE,
    }
}

地址拼接是看的天地图官网给的例子,天地图API传送门

天地图官方例子

注意

天地图现在要去官网申请一个key才能用


申请key

申请后替换上面类里的key

使用方法

使用的kotlin代码,应该能看懂吧

        //去除水印
        ArcGISRuntimeEnvironment.setLicense("runtimelite,1000,rud4449636536,none,NKMFA0PL4S0DRJE15166")
        //去除logo
        mapView.isAttributionTextVisible = false
         //默认底图
        val webTiledLayer =
            TianDiTuTiledLayerClass.CreateTianDiTuTiledLayer(
                TianDiTuTiledLayerClass.LayerType.TIANDITU_VECTOR_MERCATOR
            )
        //标注图层
        val webTiledLayerLable =
            TianDiTuTiledLayerClass.CreateTianDiTuTiledLayer(
                TianDiTuTiledLayerClass.LayerType.TIANDITU_VECTOR_MERCATOR_LABLE
            )
        val basemap = Basemap()
        basemap.baseLayers.add(webTiledLayer)
        basemap.baseLayers.add(webTiledLayerLable)
        val arcGISMap = ArcGISMap(basemap)
        mapView.map = arcGISMap

至此就大功告成,需要添加三个权限,读写文件是运行时权限,别忘了

    
    
    

我自定义类里只写了六种常用的底图类型,官方提供了
官方提供底图

官方提供底图

可自由扩展,添加LayerType的枚举类型和

public static WebTiledLayer CreateTianDiTuTiledLayer(LayerType layerType) 

方法中的case就行,
代码中的

对应官网的

代码中的

对应官网的

你可能感兴趣的:(Arcgis runtime for Android 100.5 加载天地图)