Learn how to create and display a map A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more with a basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more .

display a map

A map contains layers A layer is a reference to a collection of geographic data that is used to access and display data. The data for layers are typically provided by the basemap layer service and data services. Learn more of geographic data. A map contains a basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more and, optionally, one or more data layers A data layer is a layer that references geographic data from a file or a service and is used to visualize the data in a map or scene. Learn more . You can display a specific area of a map by using a map view A map view is a user interface that displays map layers and graphics in 2D. It controls the area (extent) of the map that is visible and supports user interactions such as pan and zoom. Learn more and setting the location A location is a position or region (point, line, or polygon) on the earth's surface. Learn more and zoom level Zoom level is a value that sets the scale for a map view or a scene view. Learn more .

In this tutorial, you create and display a map A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more of the Santa Monica Mountains in California using the topographic basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more .

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 ArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. Learn more used in this tutorial, you must implement API key authentication API key authentication is a type of authentication that uses an API key to authenticate requests to ArcGIS services and secure portal items. Learn more or user authentication User authentication is a type of authentication that allows users with an ArcGIS account to sign into an application and allow it to access ArcGIS content, services, and resources on their behalf. The typical authorization protocol used is OAuth2.0. Learn more using an ArcGIS Location Platform An ArcGIS Location Platform account, formerly known as an ArcGIS Developer account, is an identity associated with an ArcGIS Location Platform subscription. Learn more or an ArcGIS Online An ArcGIS Online account, also known as an ArcGIS Organization account, is an identity associated with an ArcGIS Online subscription. It can be used to access ArcGIS tools and develop applications with ArcGIS location services for an organization. Learn more 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 An access token is an authorization string that provides access to secure ArcGIS content, data, and services. Its capabilities are determined by the privileges it supports. It is obtained by implementing API key authentication, User authentication, or App authentication. Learn more with privileges Privileges are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. Learn more 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 are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. Learn more :

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

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

    cd display_a_map
  2. Run:

    dart pub add arcgis_maps
  3. Run:

    flutter pub upgrade
  4. Lastly, run:

    dart run arcgis_maps install
  5. Return to main.dart.

  6. Import the arcgis_maps package.

    main.dart
    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
    // ...
    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
    <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 ArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. Learn more , 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 An access token is an authorization string that provides access to secure ArcGIS content, data, and services. Its capabilities are determined by the privileges it supports. It is obtained by implementing API key authentication, User authentication, or App authentication. Learn more .

    main.dart
    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 A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more that displays the topographic basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more . 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
    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
    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
    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
    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
    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 A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more with the topographic basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more 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 ArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. Learn more , 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 An access token is an authorization string that provides access to secure ArcGIS content, data, and services. Its capabilities are determined by the privileges it supports. It is obtained by implementing API key authentication, User authentication, or App authentication. Learn more .

    main.dart
    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:

    flutter pub upgrade
  2. Run:

    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 A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more with the topographic basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more centered on the Santa Monica Mountains in California. Pinch, drag, and double-tap the map view to explore the map.