Apply scheduled updates to a downloaded preplanned map area.
Use case
With scheduled updates, the author can update the features within the preplanned areas on the service once, and multiple end-users can request these updates to bring their local copies up to date with the most recent state. Importantly, any number of end-users can download the same set of cached updates which means that this workflow is extremely scalable for large operations where you need to minimize load on the server.
This workflow can be used by survey workers operating in remote areas where network connectivity is not available. The workers could download mobile map packages to their individual devices and perform their work normally. Once they regain internet connectivity, the mobile map packages can be updated to show any new features that have been added to the online service.
How to use the sample
Start the app. It will display an offline map, check for available updates, and show update availability and size. Select 'Apply Updates' to apply the updates to the local offline map and show the results.
How it works
- Create an
OfflineMapSyncTask
with your offline map. - If desired, get
OfflineMapUpdatesInfo
from the task to check for update availability or update size. - Get a set of default
OfflineMapSyncParameters
for the task. - Set the parameters to download all available updates.
- Use the parameters to create an
OfflineMapSyncJob
. - Start the job and get the results once it completes successfully.
- Check if the mobile map package needs to be reopened, and do so if necessary.
- Finally, display your offline map to see the changes.
Relevant API
- MobileMapPackage
- OfflineMapSyncJob
- OfflineMapSyncParameters
- OfflineMapSyncResult
- OfflineMapSyncTask
- OfflineMapUpdatesInfo
Offline data
This sample uses a mobile map package, which can be found on ArcGIS Online.
The DataManager
class is used download data from an ArcGIS Online portal item to the local application data folder. Most samples use a call from the sample viewer code to download the data before starting the sample. The data is not re-downloaded if the sample is opened again. This sample uses DataManager
to delete the mobile map package data (if present) and download it again. Applying updates to the map package changes the offline data, so that data must be refreshed every time this sample is opened.
About the data
The data in this sample shows the roads and trails in the Canyonlands National Park, Utah. Data by U.S. National Parks Service. No claim to original U.S. Government works.
Additional information
Note: preplanned areas using the Scheduled Updates workflow are read-only. For preplanned areas that can be edited on the end-user device, see the Download preplanned map area sample. For more information about offline workflows, see Offline maps, scenes, and data in the ArcGIS Developers guide.
Tags
offline, pre-planned, preplanned, synchronize, update
Sample Code
// Copyright 2019 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.
using ArcGIS.Samples.Managers;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Tasks;
using Esri.ArcGISRuntime.Tasks.Offline;
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace ArcGIS.WinUI.Samples.ApplyScheduledUpdates
{
[ArcGIS.Samples.Shared.Attributes.Sample(
name: "Apply scheduled updates to preplanned map area",
category: "Map",
description: "Apply scheduled updates to a downloaded preplanned map area.",
instructions: "Start the app. It will display an offline map, check for available updates, and show update availability and size. Select 'Apply Updates' to apply the updates to the local offline map and show the results.",
tags: new[] { "offline", "pre-planned", "preplanned", "synchronize", "update" })]
[ArcGIS.Samples.Shared.Attributes.OfflineData()]
public partial class ApplyScheduledUpdates
{
// Mobile map package.
private MobileMapPackage _mobileMapPackage;
// Task used to sync the mobile map package.
private OfflineMapSyncTask _offlineMapSyncTask;
// ArcGIS online item id for the mobile map package.
private const string _itemId = "740b663bff5e4198b9b6674af93f638a";
// Path to the mobile map package.
private string _mapPackagePath;
public ApplyScheduledUpdates()
{
InitializeComponent();
_ = Initialize();
}
private async Task Initialize()
{
try
{
// Clear the exiting sample data.
Directory.Delete(DataManager.GetDataFolder(_itemId, ""), true);
}
catch (IOException)
{
// Do nothing. Exception happens when sample hasn't been run before and data isn't already present.
}
try
{
// Token for cancelling the download if the sample is exited early.
var tokenSource = new CancellationTokenSource();
CancellationToken token = tokenSource.Token;
// Add an event to close the mobile map package when the sample closes.
Unloaded += (s, e) =>
{
_mobileMapPackage?.Close();
tokenSource.Cancel();
};
// Download the mobile map package using the sample viewer's data manager.
await DataManager.DownloadDataItem(_itemId, token);
// Get the folder path to the mobile map package.
_mapPackagePath = DataManager.GetDataFolder(_itemId, "");
// Load the mobile map package.
_mobileMapPackage = new MobileMapPackage(_mapPackagePath);
await _mobileMapPackage.LoadAsync();
// Set the mapview to the map from the package.
Map offlineMap = _mobileMapPackage.Maps[0];
MyMapView.Map = offlineMap;
// Create an offline map sync task for the map.
_offlineMapSyncTask = await OfflineMapSyncTask.CreateAsync(offlineMap);
// Check if there are scheduled updates to the preplanned map area.
_ = CheckForScheduledUpdates();
}
catch (Exception ex)
{
await new MessageDialog2(ex.Message, "Error").ShowAsync();
}
}
private async Task CheckForScheduledUpdates()
{
try
{
// Get the information for offline updates.
OfflineMapUpdatesInfo info = await _offlineMapSyncTask.CheckForUpdatesAsync();
// Check if there are updates that can be downloaded.
if (info.DownloadAvailability == OfflineUpdateAvailability.Available)
{
// Get the size of the update.
double updateSize = info.ScheduledUpdatesDownloadSize / 1024;
// Update the UI.
InfoLabel.Text = $"Updates: {info.DownloadAvailability}\nUpdate size: {updateSize} kilobytes.";
ApplyButton.IsEnabled = true;
}
else
{
// Update the UI.
InfoLabel.Text = $"Updates: {info.DownloadAvailability}\nThe preplanned map area is up to date.";
ApplyButton.IsEnabled = false;
}
}
catch (Exception ex)
{
await new MessageDialog2(ex.Message, "Error").ShowAsync();
}
}
private async void ApplyButton_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
try
{
// Create default sync parameters.
OfflineMapSyncParameters parameters = await _offlineMapSyncTask.CreateDefaultOfflineMapSyncParametersAsync();
// Set the parameters to download all updates for the mobile map packages.
parameters.PreplannedScheduledUpdatesOption = PreplannedScheduledUpdatesOption.DownloadAllUpdates;
// Create a sync job using the parameters.
OfflineMapSyncJob offlineMapSyncJob = _offlineMapSyncTask.SyncOfflineMap(parameters);
// Get the results of the job.
offlineMapSyncJob.Start();
OfflineMapSyncResult result = await offlineMapSyncJob.GetResultAsync();
// Check if the job succeeded.
if (offlineMapSyncJob.Status == JobStatus.Succeeded)
{
// Check if the map package needs to be re-opened.
if (result.IsMobileMapPackageReopenRequired)
{
// Re-open the mobile map package.
_mobileMapPackage.Close();
_mobileMapPackage = new MobileMapPackage(_mapPackagePath);
await _mobileMapPackage.LoadAsync();
// Check that the mobile map package was loaded.
if (_mobileMapPackage.LoadStatus == Esri.ArcGISRuntime.LoadStatus.Loaded && _mobileMapPackage.Maps.Any())
{
// Set the mapview to the map from the package.
Map offlineMap = _mobileMapPackage.Maps[0];
MyMapView.Map = offlineMap;
// Create an offline map sync task for the map.
_offlineMapSyncTask = await OfflineMapSyncTask.CreateAsync(offlineMap);
}
else
{
await new MessageDialog2("Failed to load the mobile map package.", "Error").ShowAsync();
}
}
// Verify that the map is up to date and change the UI to reflect the update availability status.
_ = CheckForScheduledUpdates();
}
else
{
await new MessageDialog2("Error syncing the offline map.", "Error").ShowAsync();
}
}
catch (Exception ex)
{
await new MessageDialog2(ex.Message, "Error").ShowAsync();
}
}
}
}