Display a map

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

display a map

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.

Prerequisites

Before starting this tutorial:

  1. You need an ArcGIS Location Platform or ArcGIS Online account.

  2. Your system meets the system requirements.

  3. You need an IDE for Flutter - we recommend VS Code.

Set up authentication

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

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):

    • Privileges
      • Location services > Basemaps
  2. Copy and paste the API key access token into a safe location. It will be used in a later step.

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 Flutter app

  1. Open VS Code, then select Open Folder... in the welcome tab. Choose your preferred project location.

  2. Go to View > Terminal.

  3. In the terminal window, use the following command to create a new Flutter app called display_a_map. Set your required target platform(s) and an org name, com.example.app.

    Use dark colors for code blocksCopy
    1
    flutter create -e display_a_map --platforms ios,android --org com.example.app

Add ArcGIS Maps SDK for Flutter

Add a dependency to the arcgis_maps package.

  1. In VS Code's terminal, change the directory to your new project:

    Use dark colors for code blocksCopy
    1
      cd display_a_map
  2. Run:

    Use dark colors for code blocksCopy
    1
    dart pub add arcgis_maps
  3. Run:

    Use dark colors for code blocksCopy
    1
    flutter pub upgrade
  4. Lastly, run:

    Use dark colors for code blocksCopy
    1
    dart run arcgis_maps install

Platform specific configuration

  1. Update the minimum requirements by editing android/app/build.gradle.kts file in your project:

    build.gradle.kts
    Use dark colors for code blocks
    8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    Change lineChange 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
    android {
        namespace = "com.example.app.display_a_map"
        compileSdk = flutter.compileSdkVersion
        ndkVersion = "27.0.12077973"
    
        // ...
    
        defaultConfig {
            // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
            applicationId = "com.example.app.display_a_map"
            // You can update the following values to match your application needs.
            // For more information, see: https://flutter.dev/to/review-gradle-config.
            minSdk = 26
            targetSdk = flutter.targetSdkVersion
            versionCode = flutter.versionCode
            versionName = flutter.versionName
        }
    
        // ...
    
  2. Update the Kotlin version by editing android/settings.gradle.kts file in your project:

    settings.gradle.kts
    Use dark colors for code blocks
    19 20 21 22 23 24 25 26 27
    Change 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
    // ...
    
    plugins {
        id("dev.flutter.flutter-plugin-loader") version "1.0.0"
        id("com.android.application") version "8.7.0" apply false
        id("org.jetbrains.kotlin.android") version "1.9.0" apply false
    }
    
    // ...
    
  3. Add a permission to android/app/src/main/AndroidManifest.xml to access online resources:

    AndroidManifest.xml
    Use dark colors for code blocks
    2 3 4 5 6 7 8 9 10
    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
    49
    50
    
    <manifest xmlns:android="http://schemas.android.com/apk/res/android">
        <uses-permission android:name="android.permission.INTERNET" />
        <application
            android:label="display_a_map"
            android:name="${applicationName}"
            android:icon="@mipmap/ic_launcher">
    
            <!-- ... -->
    

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 VS Code, open lib/main.dart.

  2. Import the arcgis_maps package.

    main.dart
    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
    import 'package:flutter/material.dart';
    
    import 'package:arcgis_maps/arcgis_maps.dart';
    
  3. In the main() function, set the ArcGISEnvironment.apiKey value to your access token.

    main.dart
    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
    void main() {
    
      ArcGISEnvironment.apiKey = 'YOUR_ACCESS_TOKEN';
    
      runApp(const MainApp());
    }
    

    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.

  4. Instantiate MaterialApp inside runApp() and set the named argument, home, to an instance of MainApp.

    main.dart
    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
    void main() {
    
      ArcGISEnvironment.apiKey = 'YOUR_ACCESS_TOKEN';
    
      runApp(
    
        const MaterialApp(home: MainApp())
    
      );
    }
    

Add a map

Create a map that contains a topographic basemap layer. The map will be centered on the Santa Monica Mountains in California.

  1. Refactor the template MainApp class definition to extend StatefulWidget. Hover your mouse over the StatelessWidget keyword, right-click, select Refactor..., and chose Convert to StatefulWidget to refactor your code.

    main.dart
    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
    class MainApp extends StatefulWidget {
    
      const MainApp({super.key});
    
      @override
      State<MainApp> createState() => _MainAppState();
    
    }
    
    class _MainAppState extends State<MainApp> {
      @override
      Widget build(BuildContext context) {
    
        return const MaterialApp(
          home: Scaffold(body: Center(child: Text('Hello World!'))),
        );
    
      }
    }
  2. Remove the MaterialApp widget that is returned within the build method of class _MainAppState. This code was generated by the Flutter create template and will be replaced with code to return a widget that contains a map next.

    main.dart
    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
    class _MainAppState extends State<MainApp> {
      @override
      Widget build(BuildContext context) {
    
        return const MaterialApp(
          home: Scaffold(body: Center(child: Text('Hello World!'))),
        );
    
      }
    }
  3. Inside _MainAppState class define a final class member variable, _mapViewController, and initialize it with ArcGISMapViewController by calling the createController() on the ArcGISMapView class.

    main.dart
    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
    class _MainAppState extends State<MainApp> {
    
      final _mapViewController = ArcGISMapView.createController();
    
      @override
      Widget build(BuildContext context) {
    
      }
    
    }
  4. Within the build method, add an ArcGISMapView widget to the widget tree consisting of Scaffold, Column, and Expanded. Set the named argument, controllerProvider, to the class member variable _mapViewController and the onMapViewReady argument to a new method with the same name as the argument that you will define next.

    main.dart
    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
    class _MainAppState extends State<MainApp> {
    
      final _mapViewController = ArcGISMapView.createController();
    
      @override
      Widget build(BuildContext context) {
    
        return Scaffold(
          body: Column(
            children: [
              Expanded(
                child: ArcGISMapView(
                  controllerProvider: () => _mapViewController,
                  onMapViewReady: onMapViewReady,
                ),
              ),
            ],
          ),
        );
    
      }
    
    }
  5. Within _MainAppState, define a new method: onMapViewReady() that does not return anything.

    main.dart
    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
    class _MainAppState extends State<MainApp> {
    
      final _mapViewController = ArcGISMapView.createController();
    
      @override
      Widget build(BuildContext context) {
    
        return Scaffold(
          body: Column(
            children: [
              Expanded(
                child: ArcGISMapView(
                  controllerProvider: () => _mapViewController,
                  onMapViewReady: onMapViewReady,
                ),
              ),
            ],
          ),
        );
    
      }
    
      void onMapViewReady() {
    
      }
    
    }
  6. Within the new method define a final variable, map, and initialize it to an ArcGISMap with the ArcGIS Topographic basemap style.

    main.dart
    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
    class _MainAppState extends State<MainApp> {
    
      final _mapViewController = ArcGISMapView.createController();
    
      @override
      Widget build(BuildContext context) {
    
        return Scaffold(
          body: Column(
            children: [
              Expanded(
                child: ArcGISMapView(
                  controllerProvider: () => _mapViewController,
                  onMapViewReady: onMapViewReady,
                ),
              ),
            ],
          ),
        );
    
      }
    
      void onMapViewReady() {
    
        final map = ArcGISMap.withBasemapStyle(BasemapStyle.arcGISTopographic);
    
      }
    
    }
  7. Set the ArcGIS map view controller's arcGISMap property to the map. In addition, call setViewpoint() to zoom to the Santa Monica Mountains in California.

    main.dart
    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
    class _MainAppState extends State<MainApp> {
    
      final _mapViewController = ArcGISMapView.createController();
    
      @override
      Widget build(BuildContext context) {
    
        return Scaffold(
          body: Column(
            children: [
              Expanded(
                child: ArcGISMapView(
                  controllerProvider: () => _mapViewController,
                  onMapViewReady: onMapViewReady,
                ),
              ),
            ],
          ),
        );
    
      }
    
      void onMapViewReady() {
    
        final map = ArcGISMap.withBasemapStyle(BasemapStyle.arcGISTopographic);
    
        _mapViewController.arcGISMap = map;
        _mapViewController.setViewpoint(
          Viewpoint.withLatLongScale(
            latitude: 34.02700,
            longitude: -118.80500,
            scale: 72000,
          ),
        );
    
      }
    
    }

Run the app

  1. Make sure you have an Android emulator, iOS simulator or physical device configured and running.

  2. In VS Code, select Run > Run Without Debugging.

You will 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 under Solution and unzip the file to a location on your machine.

  2. Open the project in VS code.

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 VS Code, open lib/main.dart.

  2. In the main() function, set the ArcGISEnvironment.apiKey value to your access token.

    main.dart
    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
    void main() {
    
      ArcGISEnvironment.apiKey = 'YOUR_ACCESS_TOKEN';
    
      runApp(const MainApp());
    }
    

    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.

Run the application

Follow these steps to run the application.

  1. In VS Code's terminal, run:

    Use dark colors for code blocksCopy
    1
    flutter pub upgrade
  2. Run:

    Use dark colors for code blocksCopy
    1
    dart run arcgis_maps install
  3. Make sure you have an Android emulator, iOS simulator or physical device configured and running.

  4. In VS Code, select Run > Run Without Debugging.

You will 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.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.