一、修改build.gradle文件
1.1在Android窗口中打开 build.gradle(Module:app)文件
1.2添加以下代码:
// build.gradle(Module)
android {
// 其他代码
...
// 避免打包时重复或冲突
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
}
}
dependencies {
// 其他代码
...
// 添加ArcGIS依赖,此处用的版本是15,可按需更换
implementation 'com.esri.arcgisruntime:arcgis-android:100.15.0'
}
二、修改settings.gradle文件
2.1在Android窗口中打开 settings.gradle 文件
2.2在 dependencyResolutionManagement 中添加如下代码:
// settings.gradle
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
// 镜像地址
maven { url = uri("https://maven.aliyun.com/repository/central") }
maven { url = uri("https://maven.aliyun.com/repository/public") }
maven { url = uri("https://maven.aliyun.com/repository/google") }
maven { url = uri("https://maven.aliyun.com/repository/gradle-plugin") }
// 添加以下内容
maven { url = uri("https://esri.jfrog.io/artifactory/arcgis") }
}
}
三、修改AndroidManifest.xml文件
3.1在Android窗口中,打开 app -> manifests -> AndroidManifest.xml文件
3.2添加如下代码,设置联网权限及版本支持:
<!-- AndroidManifest.xml -->
<manifest
...>
// 联网权限
<uses-permission android:name="android.permission.INTERNET" />
// OpenGL版本
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
...
</application>
</manifest>
四、同步 Gradle 更改
点击 Sync now 提示,或点击工具栏中的小象图标刷新 (Sync Project with Gradle Files),初次加载可能需要较长等待时间
五、简单项目示例:
5.1在activity_main.xml中将元素替换为MapView元素:
<!-- activity_main.xml -->
<?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=".MainActivity">
<!-- 添加MapView -->
<com.esri.arcgisruntime.mapping.view.MapView
android:id="@+id/mapView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
5.2在MainActivity.kt文件中引入并使用:
// MainActivity.kt
import ...
class MainActivity : AppCompatActivity() {
// 定义 mapView 对象
private var mapView : MapView?= null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 绑定对象与控件
mapView = findViewById(R.id.mapView)
// 创建地图对象
val map = ArcGISMap(Basemap.Type.TOPOGRAPHIC, 34.056295, -117.195800, 16)
// 设置地图显示在 mapView 中
mapView?.map = map
}
override fun onPause() {
mapView.pause()
super.onPause()
}
override fun onResume() {
super.onResume()
mapView.resume()
}
override fun onDestroy() {
mapView.dispose()
super.onDestroy()
}
}
5.3运行测试,出现如下界面:
注意联网,初次运行不显示界面可以关掉Android Studio重启试试