ArcGIS for android 简单使用(一):环境配置与定位当前位置,放大,缩小地图

1. 配置开发环境

按照arcgis官网引入jar包,但是我这里用的是10.2.8版本,不是最新版本

1.1 引入仓库

在外层目录下的build.gradle文件中写入以下代码

allprojects {
  repositories {
    google()
    jcenter()

    // Add the Esri public Bintray Maven repository
    maven {
        url 'https://esri.bintray.com/arcgis'
    }
  }
}
1.2 引入sdk

然后再app目录下的build.gradle文件中加上

compile 'com.esri.arcgis.android:arcgis-android:10.2.8'

因为我不是用的最新版本的sdk,会出现一个问题
问题描述
为了避免这个问题,在android部分加上

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

环境配置好了,接下来就是arcgis的使用

2. arcgis的简单使用

新建项目,因为需要定位,所以要加入权限,同时因为地图的加载,需要设备支持openGL2.0,详细请参考官网详情

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

在布局文件中使用arcgis的MapView控件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.esri.android.map.MapView
            android:id="@+id/map_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </com.esri.android.map.MapView>
    </RelativeLayout>

    <LinearLayout
        android:layout_gravity="right|center"
        android:layout_marginEnd="20dp"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/iv_scale_plus"
            android:background="@mipmap/ic_launcher_round"
            android:layout_width="48dp"
            android:layout_height="62dp" />

        <ImageView
            android:id="@+id/iv_scale_sub"
            android:background="@mipmap/ic_launcher_round"
            android:layout_width="48dp"
            android:layout_height="62dp" />
    </LinearLayout>

</FrameLayout>

然后在Activity中实例化控件,并加载一个开放的网络图层

private MapView mapView;
/**开放图层URL**/
private String mapServerUrl = "http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer";

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mapView = findViewById(R.id.map_view);
        ArcGISTiledMapServiceLayer arcGISTiledMapServiceLayer = new ArcGISTiledMapServiceLayer(mapServerUrl);
        mapView.addLayer(arcGISTiledMapServiceLayer);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.unpause();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.pause();
    }

定位当前位置

/**
     * 定位当前位置
     */
    private void markLocation(){
        localLayer = new GraphicsLayer();
        mapView.addLayer(localLayer);

        // 获取定位服务类
        LocationDisplayManager ldManager = mapView.getLocationDisplayManager();
        ldManager.setShowLocation(true);
        // 设置定位模式
        /**
         *  LocationDisplayManager.AutoPanMode:

         (1) COMPASS:定位到你所在的位置(作为中心位置显示)并按照手机所指向的方向旋转地图(非行驶状态)。

         (2)LOCATION:自动定位到你的位置(作为中心位置显示)

         (3)NAVIGATION:默认情况下,这将图标放置在屏幕底部,并将地图旋转至行驶的方向。

         (4)OFF:不会自动定位,它只会简单地显示地图(默认)
         */
        ldManager.setAutoPanMode(LocationDisplayManager.AutoPanMode.LOCATION);
        ldManager.setShowPings(true);
        // 开始定位
        ldManager.start();
    }

放大、缩小地图,这个就很简单了,一行代码搞定

 @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_scale_plus:
                // 放大地图
                mapView.zoomin();
                break;
            case R.id.iv_scale_sub:
                // 缩小地图
                mapView.zoomout();
                break;
            default:
                break;
        }
    }

运行效果如图,界面太丑,不忍直视。图中的圆点就是当前位置。
运行效果如图

最后附上完整代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private MapView mapView;
    /**开放图层URL**/
    private String mapServerUrl = "http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer";

    /**地图放大缩小按钮*/
    private ImageView scalePlus;
    private ImageView subPlus;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mapView = findViewById(R.id.map_view);
        scalePlus = findViewById(R.id.iv_scale_plus);
        subPlus = findViewById(R.id.iv_scale_sub);
        scalePlus.setOnClickListener(this);
        subPlus.setOnClickListener(this);

        addLayer();
        markLocation();

    }

    /**
     * 加载地图图层
     */
    private void addLayer() {
        ArcGISTiledMapServiceLayer arcGISTiledMapServiceLayer = new ArcGISTiledMapServiceLayer(mapServerUrl);
        mapView.addLayer(arcGISTiledMapServiceLayer);
    }

    /**
     * 定位当前位置
     */
    private void markLocation(){

        // 获取定位服务类
        LocationDisplayManager ldManager = mapView.getLocationDisplayManager();
        ldManager.setShowLocation(true);
        // 设置定位模式
        /**
         *  LocationDisplayManager.AutoPanMode:

         (1) COMPASS:定位到你所在的位置(作为中心位置显示)并按照手机所指向的方向旋转地图(非行驶状态)。

         (2)LOCATION:自动定位到你的位置(作为中心位置显示)

         (3)NAVIGATION:默认情况下,这将图标放置在屏幕底部,并将地图旋转至行驶的方向。

         (4)OFF:不会自动定位,它只会简单地显示地图(默认)
         */
        ldManager.setAutoPanMode(LocationDisplayManager.AutoPanMode.LOCATION);
        ldManager.setShowPings(true);
        // 开始定位
        ldManager.start();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_scale_plus:
                // 放大地图
                mapView.zoomin();
                break;
            case R.id.iv_scale_sub:
                // 缩小地图
                mapView.zoomout();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.unpause();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.pause();
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值