A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developers.arcgis.com/kotlin/maps-2d/tutorials/display-a-map/ below:

Display a map | ArcGIS Maps SDK for Kotlin

Learn how to create and display a map with a basemap layer.

A map contains layers of geographic data. A map contains a basemap layer and, optionally, one or more data layers. You can display a specific area of a map by using a map view and setting the location and zoom level.

In this tutorial, you create and display a map of the Santa Monica Mountains in California using the topographic basemap layer.

The map and code will be used as the starting point for other 2D tutorials.

Mapping and location services guide

For more background information about the topics in this tutorial, visit Maps (2D) and Basemaps.

Prerequisites

Before starting this tutorial, you need the following:

  1. An ArcGIS Location Platform or ArcGIS Online account.

  2. A development and deployment environment that meets the system requirements.

  3. An IDE for Android development in Kotlin.

Note

It is recommended that you use the latest stable version Android Studio to create this tutorial app. The code described in the steps below, however, should work in any up-to-date Android IDE that supports Kotlin.

Set up authentication

To access the secure ArcGIS location services used in this tutorial, you must implement API key authentication or user authentication using an ArcGIS Location Platform or an ArcGIS Online account.

You can implement API key authentication or user authentication in this tutorial. Compare the differences below:

API key authentication

Learn more in API key authentication.

User authentication

Learn more in User authentication.

Security and authentication guide

To learn more about the different types of authentication, visit Types of authentication.

Create a new API key access token with privileges to access the secure resources used in this tutorial.

  1. Complete the Create an API key tutorial and create an API key with the following privilege(s):

  2. Copy and paste the API key access token into a safe location. It will be used in a later step.

Create new OAuth credentials to access the secure resources used in this tutorial.

  1. Complete the Create OAuth credentials for user authentication tutorial to obtain a Client ID and Redirect URL.

    A Client ID uniquely identifies your app on the authenticating server. If the server cannot find an app with the provided Client ID, it will not proceed with authentication.

    The Redirect URL (also referred to as a callback url) is used to identify a response from the authenticating server when the system returns control back to your app after an OAuth login. Since it does not necessarily represent a valid endpoint that a user could navigate to, the redirect URL can use a custom scheme, such as my-app://auth. It is important to make sure the redirect URL used in your app's code matches a redirect URL configured on the authenticating server.

  2. Copy and paste the Client ID and Redirect URL into a safe location. They will be used in a later step.

All users that access this application need account privileges to access the ArcGIS Basemap Styles service.

Develop or download

You have two options for completing this tutorial:

  1. Option 1: Develop the code or
  2. Option 2: Download the completed solution
Option 1: Develop the code Create a new Android Studio project

Use Android Studio to create an app and configure it to reference the API.

  1. Open Android Studio.

  2. In the Project tool window, make sure that your current view is Android. These tutorial instructions refer to that view.

    If your view name is something other than Android (such as Project or Packages), click on the dropdown arrow and select Android from the list.

  3. From the Android view, open Gradle Scripts > build.gradle.kts (Project: Tutorial). Replace the contents of the file with the following code.

    build.gradle.kts (Project: Tutorial)

    Use dark colors for code blocks

    1
    2
    3
    4
    5
    6
    // Top-level build file where you can add configuration options common to all sub-projects/modules.
    plugins {
        alias(libs.plugins.android.application) apply false
        alias(libs.plugins.kotlin.android) apply false
        alias(libs.plugins.kotlin.compose) apply false
    }
  4. From the Android view, open Gradle Scripts > build.gradle.kts (Module :app). Replace the contents of the file with the entire expanded code below.

    build.gradle.kts (Module: app)

    Expand

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
        // ArcGIS Maps for Kotlin - SDK dependency
        implementation(libs.arcgis.maps.kotlin)
        // Toolkit dependencies
        implementation(platform(libs.arcgis.maps.kotlin.toolkit.bom))
        implementation(libs.arcgis.maps.kotlin.toolkit.geoview.compose)
        implementation(libs.arcgis.maps.kotlin.toolkit.authentication)
    

    Kotlin 2.0+ requires the Compose Compiler Gradle plugin, which is referenced as alias(libs.plugins.kotlin.compose) in the plugins block at the top of the build.gradle.kts (Module :app) and declared as kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } in the libs.versions.toml file. The version of the Compose Compiler Gradle plugin and the Kotlin Android plugin are the same.

    If you are using a Kotlin version earlier than 2.0, you must verify that the Compose compiler and the Kotlin compiler versions are compatible. For details, see Compose to Kotlin Compatibility Map.

  5. From the Android view, open Gradle Scripts > libs.versions.toml. In the [versions] section, you need to declare the version number for ArcGIS Maps SDK for Kotlin. And in the [libraries] section, you need to add the library declarations for the following:

    The version for the Toolkit BOM applies to all the Toolkit modules you declare.

    Gradle version catalogs are the standard Android approach to declaring dependency versions. They are preferred over specifying versions numbers in the build.gradle.kts or listing version numbers in a version.gradle. In recent releases of Android Studio, the New Project Wizard generates build.gradle.kts and gradle/libs.versions.toml files that support this standard.

    Gradle version catalogs can also use BOM files to specify a single version number for all artifacts in the BOM. For more details, see Using the BOM in the README of the ArcGIS Maps SDK for Kotlin Toolkit.

    gradle/libs.versions.toml

    Use dark colors for code blocks Copy

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    
    [versions]
    arcgisMapsKotlin = "200.8.0"
    
    [libraries]
    arcgis-maps-kotlin = { group = "com.esri", name = "arcgis-maps-kotlin", version.ref = "arcgisMapsKotlin" }
    arcgis-maps-kotlin-toolkit-bom = { group = "com.esri", name = "arcgis-maps-kotlin-toolkit-bom", version.ref = "arcgisMapsKotlin" }
    arcgis-maps-kotlin-toolkit-geoview-compose = { group = "com.esri", name = "arcgis-maps-kotlin-toolkit-geoview-compose" }
    arcgis-maps-kotlin-toolkit-authentication = { group = "com.esri", name = "arcgis-maps-kotlin-toolkit-authentication" }
    
    Note

    Don't edit your libs.version.toml by hand. Instead, expand the code below, copy the entire expanded contents, and paste it into libs.version.toml file, replacing the original contents generated by the New Project Wizard.

    gradle/libs.versions.toml

    Use dark colors for code blocks

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    
    [versions]
    arcgisMapsKotlin = "200.8.0"
    
    # Version numbers added by Android Studio New Project Wizard
    agp = "8.9.2"
    kotlin = "2.1.20"
    coreKtx = "1.16.0"
    junit = "4.13.2"
    
  6. From the Android view, open Gradle Scripts > settings.gradle.kts. Replace the contents of the file with the expanded code below.

    settings.gradle.kts (Tutorial)

    Expand

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google()
            mavenCentral()
            maven { url = uri("https://esri.jfrog.io/artifactory/arcgis") }
        }
    }
    
    rootProject.name = "Display a map"
    include(":app")
  7. Sync the Gradle changes. Click the Sync now prompt or click the refresh icon (Sync Project with Gradle Files) in the toolbar. This may take several minutes.

  8. From the Android view, open app > manifests > AndroidManifest.xml. Update the Android manifest to allow internet access.

    Insert these new elements within the manifest element. Do not alter or remove any other statements.

    Depending on what ArcGIS functionality you add in future tutorials, it is likely you will need to add additional permissions to your manifest.

    AndroidManifest.xml

    Expand

    Use dark colors for code blocks

    2 2 3 4 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6

    Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
    
        <uses-permission android:name="android.permission.INTERNET" />
    
Create a map
  1. From the Android view, right click on app > kotlin+java > com.example.app, select New > package from the list. Enter com.example.app.screens as the package name. Hit Enter on your keyboard. This step creates a new package that will contain the UI files.

  2. Right click on the screens package you just created, select New > Kotlin Class/File from the list. In the pop-up window, select File and enter MainScreen as the file name. Hit Enter on your keyboard.

  3. In MainScreen.kt, delete any lines of code that were inserted automatically by Android Studio. Then add the following OptIn annotation, package name, and imports.

    Attention

    Ensure that you import the composable function com.arcgismaps.tookit.geoviewcompose.MapView, shown in the code block below. It is defined in the ArcGIS Maps SDK for Kotlin Toolkit. For Composable-enabled code, do not use the class named com.arcgismaps.mapping.view.MapView from the ArcGIS Maps SDK API.

    MainScreen.kt

    Use dark colors for code blocks

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    @file:OptIn(ExperimentalMaterial3Api::class)
    
    package com.example.app.screens
    
    import androidx.compose.foundation.layout.fillMaxSize
    import androidx.compose.foundation.layout.padding
    import androidx.compose.material3.ExperimentalMaterial3Api
    import androidx.compose.material3.Scaffold
    import androidx.compose.material3.Text
    import androidx.compose.material3.TopAppBar
    import androidx.compose.runtime.Composable
    import androidx.compose.runtime.remember
    import androidx.compose.ui.Modifier
    import androidx.compose.ui.res.stringResource
    import com.arcgismaps.mapping.ArcGISMap
    import com.arcgismaps.mapping.BasemapStyle
    import com.arcgismaps.mapping.Viewpoint
    import com.arcgismaps.toolkit.geoviewcompose.MapView
    import com.example.app.R
    
    
  4. Create a top-level function named createMap() that returns an ArcGISMap.

    MainScreen.kt

    Expand

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    fun createMap(): ArcGISMap {
    
    }
  5. Create an ArcGISMap using the BasemapStyle.ArcGISTopographic, and call apply {} on the map. The function returns this ArcGISMap.

    MainScreen.kt

    Expand

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    fun createMap(): ArcGISMap {
    
        return ArcGISMap(BasemapStyle.ArcGISTopographic).apply {
    
        }
    
    }
  6. In the apply block, create a Viewpoint with x (longitude) and y (latitude) coordinates, and a scale. Assign the view point to the initialViewpoint property of the ArcGISMap.

    MainScreen.kt

    Expand

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    fun createMap(): ArcGISMap {
    
        return ArcGISMap(BasemapStyle.ArcGISTopographic).apply {
    
            initialViewpoint = Viewpoint(
                latitude = 34.0270,
                longitude = -118.8050,
    
                scale = 72000.0
    
            )
    
        }
    
    }
Create a MainScreen to hold the map
  1. Create a composable function named MainScreen, which will call MapView.

    MainScreen.kt

    Expand

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    @Composable
    fun MainScreen() {
    
    }
    
  2. Add a remember block and call createMap() inside it. Then assign remember to a local variable named map.

    The top-level composable function remember is used to retain state across recompositions.

    MainScreen.kt

    Expand

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    @Composable
    fun MainScreen() {
    
        val map = remember {
            createMap()
        }
    
    }
    
  3. You will now call several composable functions from Android Jetpack Compose. Call Scaffold and pass a TopAppBar with a Text that contains the app name (R.string.app_name).

    MainScreen.kt

    Expand

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    @Composable
    fun MainScreen() {
    
        val map = remember {
            createMap()
        }
    
        Scaffold(
            topBar = { TopAppBar(title = { Text(text = stringResource(id = R.string.app_name)) }) }
        ) {
    
        }
    
    }
    
  4. In the trailing lambda for Scaffold, call the MapView composable defined in the ArcGIS Maps SDK for Kotlin Toolkit. Pass a Modifier that has maximum size and default padding. And pass map as the arcGISMap parameter.

    MainScreen.kt

    Expand

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    @Composable
    fun MainScreen() {
    
        val map = remember {
            createMap()
        }
    
        Scaffold(
            topBar = { TopAppBar(title = { Text(text = stringResource(id = R.string.app_name)) }) }
        ) {
    
            MapView(
                modifier = Modifier.fillMaxSize().padding(it),
                arcGISMap = map
            )
    
        }
    
    }
    
Call MainScreen inside MainActivity class
  1. Open the app > kotlin+java > com.example.app > MainActivity.kt. Delete all lines of code in the file. Then add the package declaration, import statements, and the MainActivity class.

    MainActivity.kt

    Use dark colors for code blocks

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    
    package com.example.app
    
    import android.os.Bundle
    import androidx.activity.ComponentActivity
    import androidx.activity.compose.setContent
    import androidx.activity.enableEdgeToEdge
    import com.arcgismaps.ApiKey
    import com.arcgismaps.ArcGISEnvironment
    import com.arcgismaps.httpcore.authentication.OAuthUserConfiguration
    import com.arcgismaps.toolkit.authentication.AuthenticatorState
    import com.arcgismaps.toolkit.authentication.DialogAuthenticator
    import com.example.app.screens.MainScreen
    import com.example.app.ui.theme.TutorialTheme
    
    class MainActivity : ComponentActivity() {
    
    }
  2. In the setContent block of the onCreate() lifecycle function, you will call the composable function MainScreen, with default theming applied. To do this, add onCreate() with the following code.

    MainActivity.kt

    Use dark colors for code blocks

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    class MainActivity : ComponentActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            enableEdgeToEdge()
    
            setContent {
                TutorialTheme {
                    MainScreen()
    
                }
            }
        }
    
    }
Set developer credentials

To allow your app users to access ArcGIS Location Services, use the developer credentials that you created in the Set up authentication step to authenticate requests for resources.

  1. In the Android view of Android Studio, open app > kotlin+java > com.example.app > MainActivity.

  2. In the onCreate() lifecycle method of the MainActivity class, set the ArcGISEnvironment.apiKey​ property by calling ApiKey.create(). Pass in your API key access token as a string and don't forget the double quotes. Do this before the setContent block.

    MainActivity.kt

    Expand

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    class MainActivity : ComponentActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            ArcGISEnvironment.apiKey = ApiKey.create("YOUR_ACCESS_TOKEN")
    
            enableEdgeToEdge()
    
            setContent {
                TutorialTheme {
                    MainScreen()
                }
            }
        }
    }

Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.

  1. In the Android view of Android Studio, open app > kotlin+java > com.example.app > MainActivity. In the MainActivity class, create an instance of AuthenticatorState, which is from the authentication module of ArcGIS Maps SDK for Kotlin Toolkit. Add this before the setContent block.

    MainActivity.kt

    Expand

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    class MainActivity : ComponentActivity() {
    
        private val authenticatorState = AuthenticatorState()
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            enableEdgeToEdge()
    
            setContent {
                TutorialTheme {
                    MainScreen()
    
                }
            }
        }
    
    }
  2. In the onCreate() lifecycle method of the MainActivity class, set the authenticatorState.oAuthUserConfigurations property by instantiating OAuthUserConfiguration, adding it to a list, and assigning the list. Pass in the clientId and redirectURL that you created in an earlier step.

    A redirectURL is composed of a scheme and a host component. The format for the redirect url is scheme://host. For example, if the redirect url is myscheme://myhost then the scheme is myscheme and the host is myhost. You must use the RedirectURL that you supplied for your app in the user authentication part of the Set up authentication step.

    MainActivity.kt

    Expand

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            authenticatorState.oAuthUserConfigurations = listOf(
                OAuthUserConfiguration(
    
                    portalUrl = "https://www.arcgis.com",
                    clientId = "YOUR_CLIENT_ID",
                    redirectUrl = "YOUR_REDIRECT_URL"
    
                )
            )
    
            enableEdgeToEdge()
    
            setContent {
                TutorialTheme {
                    MainScreen()
    
                }
            }
        }
    
  3. In the setContent block, call the DialogAuthenticator composable function and pass in the authenticatorState. Call DialogAuthenticator after MainScreen().

    MainActivity.kt

    Expand

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            authenticatorState.oAuthUserConfigurations = listOf(
                OAuthUserConfiguration(
    
                    portalUrl = "https://www.arcgis.com",
                    clientId = "YOUR_CLIENT_ID",
                    redirectUrl = "YOUR_REDIRECT_URL"
    
                )
            )
    
            enableEdgeToEdge()
    
            setContent {
                TutorialTheme {
                    MainScreen()
    
                    DialogAuthenticator(authenticatorState)
    
                }
            }
        }
    
  4. Open app > manifests > AndroidManifest.xml. Add an <activity> tag that declares the OAuth user sign-in activity. Set the android:scheme and android:host using the scheme and host from your RedirectURL.

    A redirectURL is composed of a scheme and a host component. The format for the redirect url is scheme://host. For example, if the redirect url is myscheme://myhost then the scheme is myscheme and the host is myhost. You must use the RedirectURL that you supplied for your app in the user authentication part of the Set up authentication step.

    AndroidManifest.xml

    Expand

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
            <activity
                android:name="com.arcgismaps.toolkit.authentication.AuthenticationActivity"
                android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
                android:exported="true"
                android:launchMode="singleTop" >
                <intent-filter>
                    <action android:name="android.intent.action.VIEW" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
    
                    <data
                        android:scheme="your_redirect_url_scheme"
                        android:host="your_redirect_url_host" />
    
                </intent-filter>
            </activity>
    

Best Practice: The OAuth credentials are stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.

Run your app
  1. Click Run > Run > app to run the app.

    In Android Studio, you have two choices for running your app: an actual Android device or the Android Emulator.

    Android device

    Connect your computer to your Android device, using USB or Wi-Fi. For more details, see How to connect your Android device.

    Android Emulator

    Create an AVD (Android Virtual Device) to run in the Android Emulator. For details, see Run apps on the Android Emulator.

    Selecting a device

    When you build and run an app in Android Studio, you must first select a device. From the Android Studio toolbar, you can access the drop-down list of your currently available devices, both virtual and physical.

    .

    If you cannot access the list on the toolbar, click Tools > Device Manager.

You should see a map with the topographic basemap layer centered on the Santa Monica Mountains in California. Pinch, drag, and double-tap the map view to explore the map.

Alternatively, you can download the tutorial solution, as follows.

Option 2: Download the solution
  1. Click the Download solution link in the right-hand side of this page.

  2. Unzip the file to a location on your machine.

  3. Run Android Studio.

  4. Go to File > Open.... Navigate to the solution folder and click Open.

    On Windows: If you are in the Welcome to Android Studio dialog, click Open and navigate to the solution folder. Then click Open.

Since the downloaded solution does not contain authentication credentials, you must add the developer credentials that you created in the Set up authentication section.

Set developer credentials in the solution

To allow your app users to access ArcGIS location services, use the developer credentials that you created in the Set up authentication step to authenticate requests for resources.

  1. In the Android view of Android Studio, open app > kotlin+java > com.example.app > MainActivity. Set the AuthenticationMode to .API_KEY.

    MainActivity.kt

    Expand

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    class MainActivity : ComponentActivity() {
    
        private enum class AuthenticationMode { API_KEY, USER_AUTH }
    
        private val authenticationMode = AuthenticationMode.API_KEY
    
  2. Set the apiKey property with your API key access token.

    MainActivity.kt

    Expand

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            when (authenticationMode) {
                AuthenticationMode.API_KEY -> {
    
                    ArcGISEnvironment.apiKey = ApiKey.create("YOUR_ACCESS_TOKEN")
    
                }
    

Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.

  1. In the Android view of Android Studio, open app > kotlin+java > com.example.app > MainActivity. Set the AuthenticationMode to .USER_AUTH.

    MainActivity.kt

    Use dark colors for code blocks

    1
    2
    3
    4
    class MainActivity : ComponentActivity() {
        private enum class AuthenticationMode { API_KEY, USER_AUTH }
    
        private val authenticationMode = AuthenticationMode.USER_AUTH
  2. Set your clientID and redirectURL values. You must use the RedirectURL that you supplied for your app in the user authentication part of the Set up authentication step.

    MainActivity.kt

    Expand

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
                AuthenticationMode.USER_AUTH -> {
                    authenticatorState.oAuthUserConfigurations = listOf(
                        OAuthUserConfiguration(
                            portalUrl = "https://www.arcgis.com",
    
                            clientId = "YOUR_CLIENT_ID",
                            redirectUrl = "YOUR_REDIRECT_URL"
    
                        )
                    )
    
  3. Open app > manifests > AndroidManifest.xml.

  4. Set the android:scheme and android:host using the scheme and host from your RedirectURL.

    A redirectURL is composed of a scheme and a host component. The format for the redirect url is scheme://host. For example, if the redirect url is myscheme://myhost then the scheme is myscheme and the host is myhost.

    AndroidManifest.xml

    Expand

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
                    <data
                        android:scheme="your_redirect_url_scheme"
                        android:host="your_redirect_url_host" />
    

Best Practice: The OAuth credentials are stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.

Run the app

Click Run > Run > app to run the app.

You should see a map with the topographic basemap layer centered on the Santa Monica Mountains in California. Pinch, drag, and double-tap the map view to explore the map.

What's next?

Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4