Skip to content

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 or user authentication using an ArcGIS Location Platform or an ArcGIS Online account.

To complete this tutorial, click on the tab in the switcher below for your authentication type of choice, either API key authentication or User 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):

    • 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... 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

Refactor the template code

When working with the ArcGIS Maps SDK for Flutter, it is very common to require updates to application state, such as making updates to the UI in response to data changes. You will implement a stateful widget so your app is ready for this.

  1. In VS Code, open lib/main.dart.

  2. Inside main(), instantiate MaterialApp within 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
    73
    74
    75
    76
    void main() {
      runApp(
    
        const MaterialApp(home: MainApp()),
    
      );
    }
    
  3. 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
    
    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!'))),
        );
    
      }
    }
  4. 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 in a later step.

    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
    class _MainAppState extends State<MainApp> {
      @override
      Widget build(BuildContext context) {
    
        return const MaterialApp(
          home: Scaffold(body: Center(child: Text('Hello World!'))),
        );
    
      }
    }

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
  5. Return to main.dart.

  6. 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
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    import 'package:arcgis_maps/arcgis_maps.dart';
    

Make platform specific configuration changes

To develop for Android, iOS, or both, the Flutter tooling sets default values for a number of configuration versions. These are pre-configured in a new project when it is created. The updates specified below are changes that are required to these defaults in order to compile an application using the current version of the ArcGIS Maps SDK for Flutter package. For additional details see our System Requirements page.

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

    build.gradle.kts
    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
    // ...
    
    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 = 28
        targetSdk = flutter.targetSdkVersion
        versionCode = flutter.versionCode
        versionName = flutter.versionName
    }
    
    // ...
    
  2. Add a permission to android/app/src/main/AndroidManifest.xml to access online resources:

    AndroidManifest.xml
    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
    
    <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.

Pass your API key access token to the ArcGISEnvironment.

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

Add a map

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

  1. Define a final class member variable inside _MainAppState class, _mapViewController, and initialize it with ArcGISMapViewController by calling the createController() on ArcGISMapView.

    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
    73
    74
    75
    76
    class _MainAppState extends State<MainApp> {
    
      final _mapViewController = ArcGISMapView.createController();
    
      @override
      Widget build(BuildContext context) {
    
      }
    
    
    }
  2. Add widgets to the widget tree within the build() consisting of Scaffold, Column, Expanded, and ArcGISMapView. Set the map view's named argument, controllerProvider, to the class member variable _mapViewController and the onMapViewReady argument to a new method with the same name as the argument. You will define the new method 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
    73
    74
    75
    76
    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,
                ),
              ),
            ],
          ),
        );
    
      }
    
    
    }
  3. Define a new method within _MainAppState, 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
    73
    74
    75
    76
      void onMapViewReady() {
    
      }
    
    
  4. Define a final variable, map, in the new method and initialize it to an ArcGISMap using 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
    73
    74
    75
    76
      void onMapViewReady() {
    
        final map = ArcGISMap.withBasemapStyle(BasemapStyle.arcGISTopographic);
    
      }
    
    
  5. 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
    73
    74
    75
    76
      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 code, you must add the authentication code and set the credentials that you created in the Set up authentication section.

Add authentication code and set developer credentials

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

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