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();    }}

更多相关文章

  1. android菜鸟学习笔记31----Android使用百度地图API(二)获取地理
  2. 【RK3399】Android(安卓)9.0 系统更改记录
  3. libxxx.so- text relocations问题的终极解决方案
  4. PermissionsDispatcher动态权限申请
  5. Android(安卓)任意位置(指空间上的位置)弹出 Toast
  6. Android高德导航自定义UI
  7. android 百度地图sdk v3.4 绘制历史轨迹
  8. Gallery初始化显示到指定位置
  9. Android(安卓)服务端将位置信息发送给客户端的实现

随机推荐

  1. Android短信----接收流程---框架层(Framew
  2. Android(安卓)Studio导入项目慢
  3. Android(安卓)DiskLruCache完全解析
  4. Android输入输出机制之来龙去脉之前生后
  5. android下usb框架系列文章---(3)Storage
  6. Android drawable state各个属性详解
  7. Android(安卓)之 LayoutInflater 全面解
  8. ANDROID PAD版本 PHONE版本 源码有什么
  9. android 开发小记3-----Android library
  10. 〖Android〗CM10.2编译错误解决