提示:本篇文章主要介绍依赖库的配置和地图显示
环境配置
将下载的离线包中arcgis-maps-kotlin-200.0.0-beta01.aar、arcgis-maps-kotlin-200.0.0-beta01.pom拷贝到项目libs文件 夹
在android studio中新建project,在build.gradle(project)中添加以下代码:
repositories {
google()
jcenter()
//添加以下代码
maven {
url 'https://esri.jfrog.io/artifactory/arcgis'
}
//
}
接着在build.gradle(Module)中添加以下依赖:
dependencies {
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.core:core-ktx:1.7.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
// 添加如下:
implementation "com.esri:arcgis-maps-kotlin:200.0.0-beta01"
}
在build.gradle(Module)中添加databinding:
repositories {
google()
mavenCentral()
maven {
url 'https://esri.jfrog.io/artifactory/arcgis'
}
}
打开AndroidManifest.xml文件,为app添加网络访问权限:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-permission android:name="android.permission.INTERNET"/>
基本设置已完成
源码如下
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.constraintlayout.widget.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.arcgismaps.mapping.view.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
MainActivity.kt
package com.example.arcgis_off_line
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.arcgismaps.ApiKey
import com.arcgismaps.ArcGISEnvironment
import com.arcgismaps.Color
import com.arcgismaps.geometry.Point
import com.arcgismaps.geometry.PolygonBuilder
import com.arcgismaps.geometry.PolylineBuilder
import com.arcgismaps.geometry.SpatialReference
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.Viewpoint
import com.arcgismaps.mapping.symbology.*
import com.arcgismaps.mapping.view.Graphic
import com.arcgismaps.mapping.view.GraphicsOverlay
import com.arcgismaps.mapping.view.MapView
import com.example.arcgis_off_line.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var activityMainBinding: ActivityMainBinding
private val mapView: MapView by lazy {
activityMainBinding.mapView
}
private fun setupMap() {
val map = ArcGISMap(BasemapStyle.ArcGISTopographic)
mapView.map = map
mapView.setViewpoint(Viewpoint(35.0270, -117.8050, 72000.0))
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
lifecycle.addObserver(mapView)
setApiKey()
setupMap()
//addGraphics()
}
private fun setApiKey() {// license with a license key
// It is not best practice to store API keys in source code. We have you insert one here
// to streamline this tutorial.
ArcGISEnvironment.apiKey = ApiKey.create("///????????")
}
private fun addGraphics() {
// create a graphics overlay and add it to the graphicsOverlays property of the map view
val graphicsOverlay = GraphicsOverlay()
mapView.graphicsOverlays.add(graphicsOverlay)
// create a point geometry with a location and spatial reference
// Point(latitude, longitude, spatial reference)
val point = Point(-118.8065, 34.0005, SpatialReference.wgs84())
// create a point symbol that is an small red circle
val simpleMarkerSymbol = SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Circle, Color.red, 10f)
// create a blue outline symbol and assign it to the outline property of the simple marker symbol
val blueOutlineSymbol = SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.fromRgba(0, 0, 255), 2f)
simpleMarkerSymbol.outline = blueOutlineSymbol
// create a graphic with the point geometry and symbol
val pointGraphic = Graphic(point, simpleMarkerSymbol)
// add the point graphic to the graphics overlay
graphicsOverlay.graphics.add(pointGraphic)
// Create a polylineBuilder with a spatial reference and add three points to it.
// Then get the polyline from the polyline builder
// create a blue line symbol for the polyline
val polylineSymbol = SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.fromRgba(0, 0, 255), 3f)
val polylineBuilder = PolylineBuilder(SpatialReference.wgs84()) {
addPoint(-118.8215, 34.0139)
addPoint(-118.8148, 34.0080)
addPoint(-118.8088, 34.0016)
}
val polyline = polylineBuilder.toGeometry()
// create a polyline graphic with the polyline geometry and symbol
val polylineGraphic = Graphic(polyline, polylineSymbol)
// add the polyline graphic to the graphics overlay
graphicsOverlay.graphics.add(polylineGraphic)
// Create a polygon builder with a spatial reference and add five vertices (points) to it.
// Then get the polygon from the polygon builder.
val polygonBuilder = PolygonBuilder(SpatialReference.wgs84()) {
addPoint(-118.8189, 34.0137)
addPoint(-118.8067, 34.0215)
addPoint(-118.7914, 34.0163)
addPoint(-118.7959, 34.0085)
addPoint(-118.8085, 34.0035)
}
val polygon = polygonBuilder.toGeometry()
// Create a red fill symbol with an alpha component of 128: values can run from 0 to 255).
// Then create the blue simple line symbol
val polygonFillSymbol =
SimpleFillSymbol(
SimpleFillSymbolStyle.Solid,
Color.fromRgba(255, 0, 0, 128),
blueOutlineSymbol
)
// create a polygon graphic from the polygon geometry and symbol
val polygonGraphic = Graphic(polygon, polygonFillSymbol)
// add the polygon graphic to the graphics overlay
graphicsOverlay.graphics.add(polygonGraphic)
}
}
模拟运行如下:
总结
基本配置很简单,按官方文档操作,基本都能成功。