Learn how to download and display an offline map for a user-defined geographical area of a web map.
Offline maps allow users to continue working when network connectivity is poor or lost. If a web map is enabled for offline use, a user can request that ArcGIS generates an offline map for a specified geographic area of interest.
In this tutorial, you will download an offline map for an area of interest from the web map of the stormwater network within Naperville, IL, USA. You can then use this offline map without a network connection.
Before starting this tutorial, you should:
Have an ArcGIS account and an API key to access ArcGIS services. If you don't have an account, sign up for free.
Optionally, you may want to install the ArcGIS Maps SDK for .NET to get access to project templates in Visual Studio (Windows only) and offline copies of the NuGet packages.
Steps
Open a Visual Studio solution
To start the tutorial, complete the Display a web map tutorial or download and unzip the solution.
Open the .sln file in Visual Studio.
If you downloaded the solution project, set your API key.
An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.
If necessary, set the API Key.
Go to your developer dashboard to get your API key.
For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.
In Visual Studio, in the Solution Explorer, click App.xaml.cs.
In the App class, add an override for the OnStartup() function to set the ApiKey property on ArcGISRuntimeEnvironment.
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
publicpartialclassApp : Application {
protectedoverridevoidOnStartup(StartupEventArgs e) {
base.OnStartup(e);
// Note: it is not best practice to store API keys in source code.// The API key is referenced here for the convenience of this tutorial. Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.ApiKey = "YOUR_API_KEY";
}
}
}
If you are developing with Visual Studio for Windows, ArcGIS Maps SDK for .NET provides a set of project templates for each supported .NET platform. These templates follow the Model-View-ViewModel (MVVM) design pattern. Install the ArcGIS Maps SDK for .NET Visual Studio Extension to add the templates to Visual Studio (Windows only). See Install and set up for details.
Update the tutorial name used in the project (optional)
The Visual Studio solution, project, and the namespace for all classes currently use the name DisplayAWebMap. Follow the steps below if you prefer the name to reflect the current tutorial. These steps are not required, your code will still work if you keep the original name.
The tutorial instructions and code use the name DisplayAnOfflineMapOnDemand for the solution, project, and namespace. You can choose any name you like, but it should be the same for each of these.
Update the name for the solution and the project.
In Visual Studio, in the Solution Explorer, right-click the solution name and choose Rename. Provide the new name for your solution.
In the Solution Explorer, right-click the project name and choose Rename. Provide the new name for your project.
Rename the namespace used by classes in the project.
In the Solution Explorer, expand the project node.
Double-click MapViewModel.cs in the Solution Explorer to open the file.
In the MapViewModel class, double-click the namespace name (DisplayAWebMap) to select it, and then right-click and choose Rename....
Provide the new name for the namespace.
Click Apply in the Rename: DisplayAWebMap window that appears in the upper-right of the code window. This will rename the namespace throughout your project.
Build the project.
Choose Build > Build solution (or press <F6>).
Get the web map item ID
You can use ArcGIS tools to create and view web maps. Use the Map Viewer to identify the web map item ID. This item ID will be used later in the tutorial.
Go to the Naperville water network in the Map Viewer in ArcGIS Online. This web map displays stormwater network within Naperville, IL, USA.
Make a note of the item ID at the end of the browser's URL. The item ID should be acc027394bc84c2fb04d1ed317aac674
Display the web map
You can display a web map using the web map's item ID. Create a map from the web map portal item, and display it in your app.
In the Visual Studio > Solution Explorer, double-click MapViewModel.cs to open the file.
The project uses the Model-View-ViewModel (MVVM) design pattern to separate the application logic (view model) from the user interface (view). MapViewModel.cs contains the view model class for the application, called MapViewModel. See the Microsoft documentation for more information about the Model-View-ViewModel pattern.
Add additional required using statements at the top of the class.
In MapViewModel.cs, modify the SetupMap() function to update the web map's item ID to that of the Naperville water network.
The existing code creates a PortalItem using the item ID and an ArcGISPortal referencing ArcGIS Online. It sets the MapViewModel.Map property to a new Map created using the PortalItem.
Click Debug > Start Debugging (or press <F5> on the keyboard) to run the app.
You should see a map of the stormwater network within Naperville, IL, USA. Use the mouse to drag, scroll, and double-click the map view to explore the map.
Specify an area of the web map to take offline
You can specify an area of the web map to take offline using either an Envelope or a Polygon. You can use a graphic to display the area on the map.
In the MapViewModel class, create a new property named GraphicsOverlays. This will be a collection of GraphicsOverlay to display a graphic of the area to take offline.
Use a SimpleLineSymbol and a SimpleFillSymbol to display a new Graphic of the offlineArea with a red outline. Add the graphic to a new GraphicsOverlay and set the MapViewModel.GraphicsOverlays property to display it in the map view.
In the Visual Studio > Solution Explorer, double-click MainWindow.xaml to open the file.
Use data binding to bind the GraphicsOverlays property of the MapViewModel to the MapView control.
Data binding and the Model-View-ViewModel (MVVM) design pattern allow you to separate the logic in your app (the view model) from the presentation layer (the view).
Click Debug > Start Debugging (or press <F5> on the keyboard) to run the app.
You should see a red outline on the stormwater network within Naperville, IL, USA. This indicates the area of the web map that you are going to take offline.
Download and display the offline map
You can generate and download an offline map for a specified area of interest using an asynchronous task. When complete, it will provide the offline map for display in your map view.
Return to the MapViewModel.cs file and add code to the SetupMap function to create an OfflineMapTask that references the online map by calling the static OfflineMapTask.CreateAsync method.
Get default parameters to generate and download the offline map. Modify them to download a read-only offline map.
This tutorial does not involve editing and updating the contents of the offline map. When an offline map is editable, metadata is stored in ArcGIS to track and synchronize edits. Setting the GenerateOfflineMapUpdateMode to NoUpdates avoids the overhead of maintaining this metadata in ArcGIS.
Create a function called GenerateJob_ProgressChanged. This function will respond when the job completes or fails and will track the precent complete as the job runs. Add a try/catch block to handle exceptions and start by getting a reference to the GenerateOfflineMapJob, passed in as the sender argument.
If the job succeeds, set the MapViewModel.Map property with the offline map result. If it fails, notify the user. If the job is running, write the percent complete to the console.
Click Debug > Start Debugging (or press <F5> on the keyboard) to run the app.
You should see an offline map for the specified area of the stormwater network within Naperville, IL, USA. Remove your network connection and you will still be able to use the mouse to drag, scroll, and double-click the map view to explore this offline map.