学习笔记之——基于ArcGIS的Android地图构建

本博文是关于采用ArcGIS来构建Android地图

 

目录

在android studio上配置ArcGIS

将地图视图添加到布局中

在mapview中设置地图

License your Runtime app

在地图上添加一个图层

参考资料


 

在android studio上配置ArcGIS

(参考资料:https://developers.arcgis.com/android/latest/guide/develop-your-first-map-app.htm

首先创建一个新的工程。

更新项目的Gradle存储库

allprojects {
    repositories {
        google()
        jcenter()

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

再配置APP下的build.gradle (Module: app)

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation project(path: ':opencv430')

    // *** ADD ***
    implementation 'com.esri.arcgisruntime:arcgis-android:100.8.0'
}

并且再android 模块中添加Java 8 compile options 

android {
  ...
  // *** ADD ***
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

然后点击

成功了

 

更新android清单文件以允许网络访问,同时也表明应用程序使用opengl2.0或更高版本。

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

 

将地图视图添加到布局中

先修改layout文件如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main3Activity">

    <com.esri.arcgisruntime.mapping.view.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </com.esri.arcgisruntime.mapping.view.MapView>

</androidx.constraintlayout.widget.ConstraintLayout>

默认情况下,所添加的Mapview不会显示任何东西,故此下一步就算定义一个地图来显示。

在mapview中设置地图

package com.example.arcgistest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.MapView;

public class MainActivity extends AppCompatActivity {

    private MapView mMapView;

    private void setupMap() {
        if (mMapView != null) {
//            ArcGISRuntimeEnvironment.setLicense(getResources().getString(R.string.arcgis_license_key));
            Basemap.Type basemapType = Basemap.Type.STREETS_VECTOR;
            double latitude = 34.0270;
            double longitude = -118.8050;
            int levelOfDetail = 13;
            ArcGISMap map = new ArcGISMap(basemapType, latitude, longitude, levelOfDetail);
            mMapView.setMap(map);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMapView = findViewById(R.id.mapView);
        setupMap();
    }

    @Override
    protected void onPause() {
        if (mMapView != null) {
            mMapView.pause();
        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mMapView != null) {
            mMapView.resume();
        }
    }

    @Override
    protected void onDestroy() {
        if (mMapView != null) {
            mMapView.dispose();
        }
        super.onDestroy();
    }
}

有几点细节需要注意的:

首先开始建立工程时,API需要是大于19的

其次,运行到手机时,可能会出现minSdkVersion版本号不对的问题,此时改一下即可。

最后,手机应该是需要连接网络才可以显示地图。

运行结果如下图所示:

License your Runtime app

从上图可以看到,有水印的存在,因此如果需要将这个功能开发的自己的app中需要授权

首先登录自己的account(https://developers.arcgis.com/dashboard

复制自己的Runtime Lite license key

然后创建一个app_settings.xml 源文件

首先添加一个源文件。如下图所示

设置如下(除了名字都是默认)

然后添加代码

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- *** ADD *** -->
    <string name="arcgis_license_key"> YOUR_LICENSE_KEY </string>
</resources>

在地图上添加一个图层

添加下面模块为trailheads(跟踪头)数据创建特征图层,并且将其写入map中

package com.example.arcgistest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.data.ServiceFeatureTable;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.MapView;

public class MainActivity extends AppCompatActivity {

    private MapView mMapView;

    private void setupMap() {
        if (mMapView != null) {
            ArcGISRuntimeEnvironment.setLicense(getResources().getString(R.string.arcgis_license_key));//去除水印之类的
            Basemap.Type basemapType = Basemap.Type.STREETS_VECTOR;
            double latitude = 34.0270;
            double longitude = -118.8050;
            int levelOfDetail = 13;
            ArcGISMap map = new ArcGISMap(basemapType, latitude, longitude, levelOfDetail);
            mMapView.setMap(map);
        }
    }

    //为trailheads(跟踪头)数据创建特征图层
    private void addTrailheadsLayer() {
        String url = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0";
        ServiceFeatureTable serviceFeatureTable = new ServiceFeatureTable(url);
        FeatureLayer featureLayer = new FeatureLayer(serviceFeatureTable);
        ArcGISMap map = mMapView.getMap();
        map.getOperationalLayers().add(featureLayer);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMapView = findViewById(R.id.mapView);
        setupMap();
        addTrailheadsLayer();
    }

    @Override
    protected void onPause() {
        if (mMapView != null) {
            mMapView.pause();
        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mMapView != null) {
            mMapView.resume();
        }
    }

    @Override
    protected void onDestroy() {
        if (mMapView != null) {
            mMapView.dispose();
        }
        super.onDestroy();
    }
}

The app should run and show features displayed on top of a basemap

 

 

 

参考资料

https://developers.arcgis.com/labs/browse/?product=android&topic=any

https://blog.csdn.net/Gary__123456/article/details/64503424

https://www.cnblogs.com/gis-luq/p/4923508.html

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值