Learn how to display a map

In this tutorial you will display a fully interactive map
Prerequisites
Before starting this tutorial:
-
You need an ArcGIS Location Platform or ArcGIS Online account.
-
Your system meets the system requirements.
-
You need an IDE for Flutter - we recommend VS Code.
Develop or Download
You have two options for completing this tutorial:
Option 1: Develop the code
-
Open the project you created by completing the Display a map tutorial.
-
Continue with the following instructions to display a map from a mobile map package (.mmpk) file.
Add a mobile map package
Add a mobile map package
-
Create or download the
MahouRivieraTrails.mmpkmobile map package. Complete the Create a mobile map package tutorial to create the package yourself. Otherwise, download the solution file: MahouRivieraTrails.mmpk. -
In VS Code, create a new folder:
assets. Place the mobile map package file within this folder, for example:assets/MahouRivieraTrails.mmpk. -
Open
pubspec.yamland add an asset entry for the mobile map package.pubspec.yaml# ...flutter:uses-material-design: trueassets:- assets/MahouRivieraTrails.mmpk
Import additional library and packages
As part of this tutorial, you will need to access resources packaged with the app. You will add core dependencies: dart:io and services.dart. dart:io provides access to an API to deal with files and directories, while services.dart will allow you to access the mobile map package file that is packaged with the app. In addition, you’ll add the path_provider package to access local files on the device.
-
In VS Code, double-click
lib\main.dartto open the file. -
From the menu bar, select View > Terminal to open a new terminal.
-
Add the
path_providerpackage to the project as a dependency running the following command:flutter pub add path_providerThe
path_providerpackage provides a Flutter plugin for finding commonly used locations on the file system. You can learn more about this package on pub.dev. -
Import
dart:io,services.dart, andpath_providerintomain.dart.main.dartimport 'dart:io';import 'package:flutter/services.dart';import 'package:path_provider/path_provider.dart';import 'package:flutter/material.dart';import 'package:arcgis_maps/arcgis_maps.dart';void main() {The
dart:iolibrary provides API to deal with files and directories. You can read more about the library here. Theservices.dartpackage provides platform services, such asrootBundlethat contains resources packaged with an app when it was built. You can learn more here.
Open the mobile map package and display a map
Select a mapMobileMapPackage class to access the mobile map package and load it to read its contents.
-
In
lib\main.dart, navigate to theonMapViewReady()and remove all the code contained within the method.main.dartvoid onMapViewReady() {final map = ArcGISMap.withBasemapStyle(BasemapStyle.arcGISTopographic);_mapViewController.arcGISMap = map;_mapViewController.setViewpoint(Viewpoint.withLatLongScale(latitude: 34.02700,longitude: -118.80500,scale: 72000,),);} -
Within
onMapViewReady()define a constant to hold the name of the mobile map package.main.dartvoid onMapViewReady() {const mobileMapPackageName = 'MahouRivieraTrails.mmpk';} -
Use
rootBundleto load the mobile map package from the project’sassetsdirectory and store the reference within a final variablemobileMapPackageData. In addition, update theonMapViewReady()method signature to denote the method as asynchronous.main.dart > onMapViewReady()void onMapViewReady() async {const mobileMapPackageName = 'MahouRivieraTrails.mmpk';// Retrieve the mobile map package from the asset bundle.final mobileMapPackageData = await rootBundle.load('assets/$mobileMapPackageName',);} -
Create a path to a temporary directory on the device to write and store the mobile map package.
main.dart > onMapViewReady()// Retrieve the mobile map package from the asset bundle.final mobileMapPackageData = await rootBundle.load('assets/$mobileMapPackageName',);// Path to a temporary directory on the device.// NOTE: This location is not backed up and is suitable for storing// caches of downloaded files, in this case, your mobile map package.final platformTempDir = await getTemporaryDirectory();// Define the path on the device to the mobile map package.final pathToFile = '${platformTempDir.absolute.path}/$mobileMapPackageName';// Create a new file using the path to the mobile map package.final mobileMapPackageFile = File(pathToFile);// Synchronously write the mobile map package to the file// on the device.mobileMapPackageFile.writeAsBytesSync(mobileMapPackageData.buffer.asUint8List(),flush: true,); -
Create a
MobileMapPackagewith the path to the file on the device and load it.main.dart > onMapViewReady()// Synchronously write the mobile map package to the file// on the device.mobileMapPackageFile.writeAsBytesSync(mobileMapPackageData.buffer.asUint8List(),flush: true,);// Create a mobile map package using the Uri to the mobile map package file.final mobileMapPackage = MobileMapPackage.withFileUri(mobileMapPackageFile.uri,);// Load the metadata for the mobile map package.await mobileMapPackage.load(); -
Set the map view controller’s ArcGIS map property to the first map in the mobile map package.
main.dart > onMapViewReady()// Load the metadata for the mobile map package.await mobileMapPackage.load();// Check that the mobile map package is not empty.if (mobileMapPackage.maps.isNotEmpty) {// Display the first map in the mobile map package in the map// view controller._mapViewController.arcGISMap = mobileMapPackage.maps.first;}
Run the app
Follow these steps to run the application.
-
In VS Code’s terminal, run:
flutter pub upgrade -
Run:
dart run arcgis_maps install -
Make sure you have an Android emulator, iOS simulator or physical device configured and running.
-
In VS Code, select Run > Run Without Debugging.
You should see a map
Alternatively, you can download the tutorial solution, as follows.
Option 2: Download the solution
-
Click the
Download solutionlink underSolutionand unzip the file to a location on your machine. -
Open the project in VS code.
Run the application
Follow these steps to run the application.
-
In VS Code’s terminal, run:
flutter pub upgrade -
Run:
dart run arcgis_maps install -
Make sure you have an Android emulator, iOS simulator or physical device configured and running.
-
In VS Code, select Run > Run Without Debugging.
You should see a map