学习ArcGIS Maps SDK for Kotlin(200)系列 003-android studio离线环境搭建

本文详细介绍了如何在Android Studio中搭建ArcGIS Maps SDK for Kotlin的离线开发环境,包括将aar和pom文件放入libs目录,修改build.gradle文件添加依赖,配置databinding以及在AndroidManifest.xml中添加网络权限。通过这些步骤,可以成功建立基本的开发配置。
摘要由CSDN通过智能技术生成

提示:本篇文章主要介绍依赖库的配置和地图显示


环境配置

将下载的离线包中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)
    }

}

模拟运行如下:
在这里插入图片描述

总结

基本配置很简单,按官方文档操作,基本都能成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值