Arcgis For Android 加载地图以及叠加天地图服务

大家既然能看到这篇Arcgis博客说明已经对它有一定了解和认知,这边我就不做过多的介绍。咱们直接上手
!!!

1.环境配置

在项目下build.gradle的repositories目录内添加

maven {
     url 'https://esri.bintray.com/arcgis'
	}

在module的build.gradle的android目录内添加

packagingOptions {
	exclude 'META-INF/LGPL2.1'
	exclude 'META-INF/LICENSE'
	exclude 'META-INF/NOTICE'
   }

as3.0以上

implementation 'com.esri.arcgis.android:arcgis-android:10.2.9';
as3.0以下再则将implementation替换compile即可;

最后还要在AndroidManifest.xml添加权限以及配置文件。





2.加载配置

  • 在xml引入布局


  • 进行一些初始化
MapView   esrimapView=findViewById(R.id.esri_mapView);
//网络图层地址
String MapUrl="https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer";
//图层的创建
ArcGISTiledMapServiceLayer mapServiceLayer = new ArcGISTiledMapServiceLayer(MapUrl);
//图层的添加
esrimapView.addLayer(mapServiceLayer);
or下面这是本地的加载 这边就举个例子吧 tpk文件 
//同样的我们需要先拿到这个文件的地址
String path=Environment.getExternalStorageDirectory().getAbsolutePath() + "具体的文件地址和名字名字"; 
例如/Pictures/tpk_20190116.tpk
// 离线地图调用
ArcGISLocalTiledLayer arcGISTiledMapServiceLayer = new ArcGISLocalTiledLayer(path);
esriMapView.addLayer(arcGISTiledMapServiceLayer);

Arcgis For Android 加载地图以及叠加天地图服务_第1张图片
当然这个界面不是很美观,下面进行一些简单的优化。

  • 去除背后的网格
esrimapView.setMapBackground(0xffffff, 0xffffff, 1.0f, 100.0f);
  • 去除水印(Licensed for Developer User Only)
String clientID = "273DobVpQjOHcrZe";
ArcGISRuntime.setClientId(clientID);

Arcgis For Android 加载地图以及叠加天地图服务_第2张图片
在之前的xml再添加下面来设置显示范围

http://www.gpsspg.com/maps.htm 通过在线地图经纬度查询,显示初始化范围以及缩放比例
mapoptions.MapType="topo"
mapoptions.ZoomLevel="4"
mapoptions.center="30.088774362326117,106.11987703125003"

Arcgis For Android 加载地图以及叠加天地图服务_第3张图片

添加点线面

  • 采用墨卡托坐标
Point point = new Point(x,y);
//初始化线条的颜色以及大小样式
Graphic graphic = new Graphic(point, new SimpleMarkerSymbol(Color.RED, 15, SimpleMarkerSymbol.STYLE.CIRCLE));
//GraphicsLayer是图形图层,可以自定义图形添加到地图上。 
GraphicsLayer gLayerPos = new GraphicsLayer();
//添加图形
gLayerPos.addGraphic(graphic);
esrimapView.addLayer(gLayerPos);

Arcgis For Android 加载地图以及叠加天地图服务_第4张图片

  • 线采用墨卡托坐标,两点确定一条直线。
//存放坐标
List pointList=new ArrayList<>();
//起点
Point pointstart = new Point(x,y);
//末点
Point pointend= new Point(x,y);
//添加集合去
pointList.add(pointstart);
pointList.add(pointend);
Polyline polyline = new Polyline();
//简单的判断逻辑 
if (pointList.size() > 1) {
for (int i = 0; i < pointList.size(); i++) {
if (i == 0) {
	polyline.startPath(pointList.get(i));
} else {
polyline.lineTo(pointList.get(i));
		}
	}
}
Graphic graphic = new Graphic(polyline, new SimpleLineSymbol(Color.BLUE, 3, SimpleLineSymbol.STYLE.SOLID));
gLayerPos.addGraphic(graphic);
esrimapView.addLayer(gLayerPos);

Arcgis For Android 加载地图以及叠加天地图服务_第5张图片

  • 采用墨卡托坐标,三点就可以定面
	//与线差不多一样的逻辑数据无非这里要添加3个以上
	List pointList=new ArrayList<>();
	//起点
	Point pointstart = new Point(x,y);
	//中间
	Point pointmid= new Point(x,y);
	//末点
	Point pointend= new Point(x,y);
	pointList.add(pointstart);
	pointList.add(pointmid);
	pointList.add(pointend);
	Polygon polygon = new Polygon();
    for (int i = 0; i < pointList.size(); i++) {
    if (i == 0) {
    polygon.startPath(pointList.get(i)); //起点
    } else {
     polygon.lineTo(pointList.get(i));
    }
    }
    //   外线颜色
	SimpleLineSymbol lineSymbol = new SimpleLineSymbol(Color.RED, 3,SimpleLineSymbol.STYLE.SOLID);
	//   内颜色
    SimpleFillSymbol fillSymbol = new SimpleFillSymbol(Color.YELLOW);
    illSymbol.setOutline(lineSymbol);

Arcgis For Android 加载地图以及叠加天地图服务_第6张图片

  • 图片采用墨卡托坐标
PictureMarkerSymbollocationSymbol locationSymbol= new PictureMarkerSymbol(Drawable drawable);
Point point = new Point(x, y);
Graphic graphicimage = new Graphic(point, locationSymbol);
gLayerPos.addGraphic(graphicimage);esrimapView.addLayer(gLayerPos);

点线面距离面积周长计算

https://blog.csdn.net/qq_36699930/article/details/80005950 可参考这篇博客

天地图服务

我这个项目采用的是墨卡托坐标3857 如果需要添加天地图服务你的底图文件一定得和坐标系是一致的;不然是不会加载出,来对于第一次上手项目的来说,我认为这是个坑。
常量类

public enum TianDiTuTiledMapServiceType {
    /**
     * 天地图矢量
     * */
    VEC_C,
    /**
     * 天地图影像
     * */
    IMG_C,
    /**
     * 天地图矢量标注
     * */
    CVA_C,
    /**
     * 天地图影像标注
     * */
    CIA_C
}

地图地址

import java.util.Random;

public class TDTUrl {
    private TianDiTuTiledMapServiceType _tiandituMapServiceType;
    private int _level;
    private int _col;
    private int _row;

    public TDTUrl(int level, int col, int row, TianDiTuTiledMapServiceType tiandituMapServiceType) {
        this._level = level;
        this._col = col;
        this._row = row;
        this._tiandituMapServiceType = tiandituMapServiceType;
    }

    public String getUrl() {
        StringBuilder url1 = new StringBuilder("http://t");
        Random random = new Random();
        int subdomain = (random.nextInt(6) + 1);

        url1.append(subdomain);
        switch (this._tiandituMapServiceType) {
            case VEC_C://矢量
                url1.append(".tianditu.com/DataServer?T=").append("vec_w").append("&X=").append(this._col).append("&Y=")
                        .append(this._row).append("&L=").append(this._level)
                        .append("&tk=2ce94f67e58faa24beb7cb8a09780552");
                break;
            case CVA_C://矢量注记
                url1.append(".tianditu.com/DataServer?T=").append("cva_w").append("&X=").append(this._col).append("&Y=")
                        .append(this._row).append("&L=").append(this._level)
                        .append("&tk=2ce94f67e58faa24beb7cb8a09780552");
                break;
            case CIA_C://影像注记
                url1.append(".tianditu.com/DataServer?T=").append("cia_w").append("&X=").append(this._col).append("&Y=")
                        .append(this._row).append("&L=").append(this._level)
                        .append("&tk=2ce94f67e58faa24beb7cb8a09780552");
                break;
            case IMG_C://影像
                url1.append(".tianditu.com/DataServer?T=").append("img_w").append("&X=").append(this._col).append("&Y=")
                        .append(this._row).append("&L=").append(this._level)
                        .append("&tk=2ce94f67e58faa24beb7cb8a09780552");

                break;
            default:
                break;
        }
            //System.out.println(url1.toString());
            return url1.toString();

    }
}

重写TiledServiceLayer

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.esri.core.io.UserCredentials;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.RejectedExecutionException;

public class TianDiTuTiledMapServiceLayer extends TiledServiceLayer {
    private TianDiTuTiledMapServiceType _mapType;
    private TileInfo tiandituTileInfo;
    public TianDiTuTiledMapServiceLayer() {
        this(null, null,true);
    }
    public TianDiTuTiledMapServiceLayer(TianDiTuTiledMapServiceType mapType){
        this(mapType, null,true);
    }

    public TianDiTuTiledMapServiceLayer(TianDiTuTiledMapServiceType mapType,UserCredentials usercredentials){
        this(mapType, usercredentials, true);
    }
    public TianDiTuTiledMapServiceLayer(TianDiTuTiledMapServiceType mapType, UserCredentials usercredentials, boolean flag){
        super("");
        this._mapType=mapType;
        setCredentials(usercredentials);

        if(flag)
            try
            {
                getServiceExecutor().submit(new Runnable() {

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

                    final TianDiTuTiledMapServiceLayer a;


                    {
                        a = TianDiTuTiledMapServiceLayer.this;
                        //super();
                    }
                });
                return;
            }
            catch(RejectedExecutionException _ex) { }
    }
    public TianDiTuTiledMapServiceType getMapType(){
        return this._mapType;
    }
    protected void initLayer(){
        if (getID() == 0L) {
            nativeHandle = create();
            changeStatus(com.esri.android.map.event.OnStatusChangedListener.STATUS
                    .fromInt(-1000));
        }else {
            this.buildTileInfo();
            this.setFullExtent(new Envelope(-22041257.773878,
                    -20851350.0432886, 22041257.773878, 20851350.0432886));
            this.setDefaultSpatialReference(SpatialReference.create(3857));
            super.initLayer();
        }
    }
    public void refresh()
    {
        try
        {
            getServiceExecutor().submit(new Runnable() {

                public final void run()
                {
                    if(a.isInitialized())
                        try
                        {
                            a.b();
                            a.clearTiles();
                            return;
                        }
                        catch(Exception exception)
                        {
                            Log.e("ArcGIS", "Re-initialization of the layer failed.", exception);
                        }
                }

                final TianDiTuTiledMapServiceLayer a;


                {
                    a = TianDiTuTiledMapServiceLayer.this;
                    //super();
                }
            });
            return;
        }
        catch(RejectedExecutionException _ex)
        {
            return;
        }
    }
    final void b()
            throws Exception
    {

    }

    @Override
    protected byte[] getTile(int level, int col, int row) throws Exception {
        /**
         *
         * */

        byte[] result = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            URL sjwurl = new URL(this.getTianDiMapUrl(level, col, row));
            HttpURLConnection httpUrl = null;
            BufferedInputStream bis = null;
            byte[] buf = new byte[1024];

            httpUrl = (HttpURLConnection) sjwurl.openConnection();
            httpUrl.connect();
            bis = new BufferedInputStream(httpUrl.getInputStream());

            while (true) {
                int bytes_read = bis.read(buf);
                if (bytes_read > 0) {
                    bos.write(buf, 0, bytes_read);
                } else {
                    break;
                }
            };
            bis.close();
            httpUrl.disconnect();

            result = bos.toByteArray();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return result;
    }


    @Override
    public TileInfo getTileInfo(){
        return this.tiandituTileInfo;
    }
    /**
     *
     * */
    private String getTianDiMapUrl(int level, int col, int row){

        String url=new TDTUrl(level,col,row,this._mapType).getUrl();
        return url;
    }

    private void buildTileInfo()
    {
        Point origin = new Point(-20037508.342787, 20037508.342787);

         double[] scales = new double[] {591657527.591555,
                295828763.79577702, 147914381.89788899, 73957190.948944002,
                36978595.474472001, 18489297.737236001, 9244648.8686180003,
                4622324.4343090001, 2311162.217155, 1155581.108577, 577790.554289,
                288895.277144, 144447.638572, 72223.819286, 36111.909643,
                18055.954822, 9027.9774109999998, 4513.9887049999998, 2256.994353,
                1128.4971760000001};
         double[] resolutions = new double[] {156543.03392800014,
                78271.516963999937, 39135.758482000092, 19567.879240999919,
                9783.9396204999593, 4891.9698102499797, 2445.9849051249898,
                1222.9924525624949, 611.49622628138, 305.748113140558,
                152.874056570411, 76.4370282850732, 38.2185141425366,
                19.1092570712683, 9.55462853563415, 4.7773142679493699,
                2.3886571339746849, 1.1943285668550503, 0.59716428355981721,
                0.29858214164761665 };
        int levels=21;
        int dpi=96;
        int tileWidth=256;
        int tileHeight=256;
        this.tiandituTileInfo=new com.esri.android.map.TiledServiceLayer.TileInfo(origin, scales, resolutions, scales.length, dpi, tileWidth,tileHeight);
        this.setTileInfo(this.tiandituTileInfo);
    }
}
        /**
         * 天地图矢量
         * */
        TianDiTuTiledMapServiceLayer tianDiTuTiledMapServiceLayer=new TianDiTuTiledMapServiceLayer(TianDiTuTiledMapServiceType.VEC_C);
        esriMapView.addLayer(tianDiTuTiledMapServiceLayer,0);
        /**
         * 天地图矢量标注
         * */
        TianDiTuTiledMapServiceLayer serviceLayer=new TianDiTuTiledMapServiceLayer(TianDiTuTiledMapServiceType.CVA_C);
        esriMapView.addLayer(serviceLayer,1);

完成。

你可能感兴趣的:(Arcgis)