Beispiel #1
0
        public static async Task <Map> DownloadMapAsync(MapArea mapArea)
        {
            mapArea.IsDownloading = true;

            var downloadParams =
                await mapArea.OfflineTask.CreateDefaultDownloadPreplannedOfflineMapParametersAsync(mapArea.Payload);

            downloadParams.ContinueOnErrors = true;
            downloadParams.IncludeBasemap   = true; //not sure why this seems to be ignored

            //downloadParams.ReferenceBasemapDirectory = GetOfflineMapPath("reference_base_map", true);
            //downloadParams.ReferenceBasemapFilename = "???";

            string path = GetOfflineMapPath(mapArea.Id);

            DownloadPreplannedOfflineMapJob preplannedMapJob =
                mapArea.OfflineTask.DownloadPreplannedOfflineMap(downloadParams, path);

            var preplannedMapResult = await preplannedMapJob.GetResultAsync();

            if (!preplannedMapResult.HasErrors)
            {
                // Job completed successfully and all content was generated.
                Debug.WriteLine("Map " +
                                preplannedMapResult.MobileMapPackage.Item.Title +
                                " was saved to " +
                                preplannedMapResult.MobileMapPackage.Path);
            }
            else
            {
                // Job is finished but one or more layers or tables had errors.
                if (preplannedMapResult.LayerErrors.Count > 0)
                {
                    // Show layer errors.
                    foreach (var layerError in preplannedMapResult.LayerErrors)
                    {
                        Debug.WriteLine("Error occurred when taking " +
                                        layerError.Key.Name +
                                        " offline. Error : " +
                                        layerError.Value.Message);
                    }
                }
                if (preplannedMapResult.TableErrors.Count > 0)
                {
                    // Show table errors.
                    foreach (var tableError in preplannedMapResult.TableErrors)
                    {
                        Debug.WriteLine("Error occurred when taking " +
                                        tableError.Key.TableName +
                                        " offline. Error : " +
                                        tableError.Value.Message);
                    }
                }
            }

            mapArea.IsDownloading = false;

            return(preplannedMapResult.OfflineMap);
        }
 private void Job_ProgressChanged(object sender, EventArgs e)
 {
     // Because the event is raised on a background thread, the dispatcher must be used to
     // ensure that UI updates happen on the UI thread.
     InvokeOnMainThread(() =>
     {
         // Update the UI with the progress.
         DownloadPreplannedOfflineMapJob downloadJob = sender as DownloadPreplannedOfflineMapJob;
         _helpLabel.Text = $"Downloading map area... ({downloadJob.Progress}%).";
     });
 }
Beispiel #3
0
 private void OnJobProgressChanged(object sender, EventArgs e)
 {
     // Because the event is raised on a background thread, the dispatcher must be used to
     // ensure that UI updates happen on the UI thread.
     DispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal, () =>
     {
         // Update the UI with the progress.
         DownloadPreplannedOfflineMapJob downloadJob = sender as DownloadPreplannedOfflineMapJob;
         ProgressBar.Value   = downloadJob.Progress;
         BusyPercentage.Text = $"{downloadJob.Progress}%";
     });
 }
 private void OnJobProgressChanged(object sender, EventArgs e)
 {
     // Because the event is raised on a background thread, the dispatcher must be used to
     // ensure that UI updates happen on the UI thread.
     Device.BeginInvokeOnMainThread(() =>
     {
         // Update the UI with the progress.
         DownloadPreplannedOfflineMapJob downloadJob = sender as DownloadPreplannedOfflineMapJob;
         ProgressView.Progress = downloadJob.Progress / 100.0;
         BusyText.Text         = $"Downloading map... {downloadJob.Progress}%";
     });
 }
 private void OnJobProgressChanged(object sender, EventArgs e)
 {
     // Because the event is raised on a background thread, the dispatcher must be used to
     // ensure that UI updates happen on the UI thread.
     RunOnUiThread(() =>
     {
         // Update the UI with the progress.
         DownloadPreplannedOfflineMapJob downloadJob = sender as DownloadPreplannedOfflineMapJob;
         _progressBar.SetProgress(downloadJob.Progress, true);
         _progressView.SetMessage($"{downloadJob.Progress}%");
     });
 }
 private async void OnJobProgressChanged(object sender, EventArgs e)
 {
     // Because the event is raised on a background thread, the dispatcher must be used to
     // ensure that UI updates happen on the UI thread.
     await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
     {
         // Update the UI with the progress.
         DownloadPreplannedOfflineMapJob downloadJob = sender as DownloadPreplannedOfflineMapJob;
         ProgressBar.Value   = downloadJob.Progress;
         BusyPercentage.Text = $"{downloadJob.Progress}%";
     });
 }
Beispiel #7
0
        private void OnJobProgressChanged(object sender, EventArgs e)
        {
            // Get the download job.
            DownloadPreplannedOfflineMapJob downloadJob = sender as DownloadPreplannedOfflineMapJob;

            if (downloadJob == null)
            {
                return;
            }

            // UI work needs to be done on the UI thread.
            RunOnUiThread(() =>
            {
                // Update the UI with the load progress.
                _progressIndicator.SetMessage($"Downloading map area... ({downloadJob.Progress}%)");
            });
        }
Beispiel #8
0
        private void OnJobProgressChanged(object sender, EventArgs e)
        {
            // Get the download job.
            DownloadPreplannedOfflineMapJob downloadJob = sender as DownloadPreplannedOfflineMapJob;

            if (downloadJob == null)
            {
                return;
            }

            // UI work needs to be done on the UI thread.
            InvokeOnMainThread(() =>
            {
                // Update UI with the load progress.
                _progressIndicator.UpdateMessageAndProgress("Downloading map area", downloadJob.Progress);
            });
        }
        private void OnJobProgressChanged(object sender, EventArgs e)
        {
            // Get the download job.
            DownloadPreplannedOfflineMapJob downloadJob = sender as DownloadPreplannedOfflineMapJob;

            if (downloadJob == null)
            {
                return;
            }

            // UI work needs to be done on the UI thread.
            Device.BeginInvokeOnMainThread(() =>
            {
                // Update UI with the load progress.
                ProgressBar.Progress = downloadJob.Progress / 100.0;
                BusyText.Text        = "Downloading: " + downloadJob.Progress + "%";
            });
        }
        private async void OnJobProgressChanged(object sender, EventArgs e)
        {
            // Get the download job.
            DownloadPreplannedOfflineMapJob downloadJob = sender as DownloadPreplannedOfflineMapJob;

            if (downloadJob == null)
            {
                return;
            }

            // UI work needs to be done on the UI thread.
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                // Update UI with the load progress.
                ProgressBar.Value   = downloadJob.Progress;
                BusyPercentage.Text = downloadJob.Progress + "%";
            });
        }
Beispiel #11
0
        private async Task DownloadMapAreaAsync(PreplannedMapArea mapArea)
        {
            // Set up UI for download.
            _progressIndicator.SetMessage("Downloading map area...");
            _progressIndicator.SetTitle("Downloading");
            _progressIndicator.Show();

            // Get the path for the downloaded map area.
            string path = Path.Combine(_offlineDataFolder, mapArea.PortalItem.Title);

            // If the map area is already downloaded, open it and don't download it again.
            if (Directory.Exists(path))
            {
                MobileMapPackage localMapArea = await MobileMapPackage.OpenAsync(path);

                try
                {
                    // Load the map area.
                    _myMapView.Map = localMapArea.Maps.First();

                    // Update the UI.
                    _progressIndicator.Dismiss();

                    // Return without downloading the item again.
                    return;
                }
                catch (Exception)
                {
                    // Do nothing, continue as if the map wasn't downloaded.
                }
            }

            // Create the job that is used to download the map area.
            DownloadPreplannedOfflineMapJob job = _offlineMapTask.DownloadPreplannedOfflineMap(mapArea, path);

            // Subscribe to progress change events to support showing a progress bar.
            job.ProgressChanged += OnJobProgressChanged;

            try
            {
                // Download the map area.
                DownloadPreplannedOfflineMapResult results = await job.GetResultAsync();

                // Handle possible errors and show them to the user.
                if (results.HasErrors)
                {
                    StringBuilder errorBuilder = new StringBuilder();

                    // Add layer errors to the message.
                    foreach (KeyValuePair <Layer, Exception> layerError in results.LayerErrors)
                    {
                        errorBuilder.AppendLine($"{layerError.Key.Name} {layerError.Value.Message}");
                    }

                    // Add table errors to the message.
                    foreach (KeyValuePair <FeatureTable, Exception> tableError in results.TableErrors)
                    {
                        errorBuilder.AppendLine($"{tableError.Key.TableName} {tableError.Value.Message}");
                    }

                    // Show the error message.
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.SetMessage(errorBuilder.ToString()).SetTitle("Warning!").Show();
                }

                // Show the Map in the MapView.
                _myMapView.Map = results.OfflineMap;
            }
            catch (Exception ex)
            {
                // Report exception.
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.SetMessage(ex.Message).SetTitle("Downloading map area failed.").Show();
            }
            finally
            {
                // Clear the loading UI.
                _progressIndicator.Dismiss();
            }
        }
        private async Task DownloadMapAreaAsync(PreplannedMapArea mapArea)
        {
            // Show the download UI.
            _progressIndicator.UpdateMessageAndProgress("Downloading map area", 0);
            View.AddSubview(_progressIndicator);

            // Get the path for the downloaded map area.
            var path = Path.Combine(_offlineDataFolder, mapArea.PortalItem.Title);

            // If the area is already downloaded, open it and don't download it again.
            if (Directory.Exists(path))
            {
                var localMapArea = await MobileMapPackage.OpenAsync(path);

                try
                {
                    // Load the map.
                    _myMapView.Map = localMapArea.Maps.First();

                    // Reset the UI.
                    _progressIndicator.RemoveFromSuperview();

                    // Return without proceeding to download.
                    return;
                }
                catch (Exception)
                {
                    // Do nothing, continue as if map wasn't downloaded.
                }
            }

            // Create the job that is used to do the download.
            DownloadPreplannedOfflineMapJob job = _offlineMapTask.DownloadPreplannedOfflineMap(mapArea, path);

            // Subscribe to progress change events to support showing a progress bar.
            job.ProgressChanged += OnJobProgressChanged;

            try
            {
                // Download the map area.
                DownloadPreplannedOfflineMapResult results = await job.GetResultAsync();

                // Handle possible errors and show them to the user.
                if (results.HasErrors)
                {
                    var errorBuilder = new StringBuilder();

                    // Add layer errors to the message.
                    foreach (KeyValuePair <Layer, Exception> layerError in results.LayerErrors)
                    {
                        errorBuilder.AppendLine($"{layerError.Key.Name} {layerError.Value.Message}");
                    }

                    // Add table errors to the message.
                    foreach (KeyValuePair <FeatureTable, Exception> tableError in results.TableErrors)
                    {
                        errorBuilder.AppendLine($"{tableError.Key.TableName} {tableError.Value.Message}");
                    }

                    // Show the message.
                    UIAlertController alertController = UIAlertController.Create("Warning!", errorBuilder.ToString(), UIAlertControllerStyle.Alert);
                    alertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                    PresentViewController(alertController, true, null);
                }

                // Show the Map in the MapView.
                _myMapView.Map = results.OfflineMap;
            }
            catch (Exception ex)
            {
                // Report the exception.
                UIAlertController alertController = UIAlertController.Create("Downloading map area failed.", ex.Message, UIAlertControllerStyle.Alert);
                alertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                PresentViewController(alertController, true, null);
            }
            finally
            {
                // Clear the loading UI.
                _progressIndicator.RemoveFromSuperview();
            }
        }
        private async Task DownloadMapAreaAsync(PreplannedMapArea mapArea)
        {
            // Configure UI for the download.
            DownloadNotificationText.Visibility = Visibility.Collapsed;
            ProgressBar.IsIndeterminate         = false;
            ProgressBar.Value        = 0;
            BusyText.Text            = "Downloading map area...";
            BusyIndicator.Visibility = Visibility.Visible;
            MyMapView.Visibility     = Visibility.Visible;

            // Get the path for the downloaded map area.
            string path = Path.Combine(_offlineDataFolder, mapArea.PortalItem.Title);

            // If the area is already downloaded, open it and don't download it again.
            if (Directory.Exists(path))
            {
                MobileMapPackage localMapArea = await MobileMapPackage.OpenAsync(path);

                try
                {
                    // Load the map.
                    MyMapView.Map = localMapArea.Maps.First();

                    // Update the UI.
                    BusyText.Text            = string.Empty;
                    BusyIndicator.Visibility = Visibility.Collapsed;

                    // Return and don't proceed to download.
                    return;
                }
                catch (Exception)
                {
                    // Do nothing, continue as if map wasn't downloaded.
                }
            }

            // Create job that is used to download the map area.
            DownloadPreplannedOfflineMapJob job = _offlineMapTask.DownloadPreplannedOfflineMap(mapArea, path);

            // Subscribe to progress change events to support showing a progress bar.
            job.ProgressChanged += OnJobProgressChanged;

            try
            {
                // Download the map area.
                DownloadPreplannedOfflineMapResult results = await job.GetResultAsync();

                // Handle possible errors and show them to the user.
                if (results.HasErrors)
                {
                    StringBuilder errorBuilder = new StringBuilder();

                    // Add layer errors to the message.
                    foreach (KeyValuePair <Layer, Exception> layerError in results.LayerErrors)
                    {
                        errorBuilder.AppendLine(string.Format("{0} {1}", layerError.Key.Name,
                                                              layerError.Value.Message));
                    }

                    // Add table errors to the message.
                    foreach (KeyValuePair <FeatureTable, Exception> tableError in results.TableErrors)
                    {
                        errorBuilder.AppendLine(string.Format("{0} {1}", tableError.Key.TableName,
                                                              tableError.Value.Message));
                    }

                    // Show the message.
                    MessageDialog message = new MessageDialog(errorBuilder.ToString(), "Warning!");
                    await message.ShowAsync();
                }

                // Show the Map in the MapView.
                MyMapView.Map = results.OfflineMap;
            }
            catch (Exception ex)
            {
                // Report exception.
                MessageDialog message = new MessageDialog(ex.Message, "Downloading map areas failed");
                await message.ShowAsync();
            }
            finally
            {
                // Clear the loading UI.
                BusyText.Text            = string.Empty;
                BusyIndicator.Visibility = Visibility.Collapsed;
            }
        }
        private async Task DownloadMapAreaAsync(PreplannedMapArea mapArea)
        {
            // Set up UI for downloading.
            ProgressBar.IsIndeterminate = false;
            ProgressBar.Value           = 0;
            BusyText.Text            = "Downloading map area...";
            BusyIndicator.Visibility = Visibility.Visible;

            // Create folder path where the map package will be downloaded.
            string path = Path.Combine(_offlineDataFolder, mapArea.PortalItem.Title);

            // If the area is already downloaded, open it.
            if (Directory.Exists(path))
            {
                try
                {
                    // Open the offline map package.
                    MobileMapPackage localMapArea = await MobileMapPackage.OpenAsync(path);

                    // Display the first map.
                    MyMapView.Map = localMapArea.Maps.First();

                    // Update the UI.
                    BusyText.Text            = string.Empty;
                    BusyIndicator.Visibility = Visibility.Collapsed;
                    MessageLabel.Text        = "Opened offline area.";
                    return;
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    await new MessageDialog(e.Message, "Couldn't open offline area. Proceeding to take area offline.").ShowAsync();
                }
            }

            // Create download parameters.
            DownloadPreplannedOfflineMapParameters parameters = await _offlineMapTask.CreateDefaultDownloadPreplannedOfflineMapParametersAsync(mapArea);

            // Create the job.
            DownloadPreplannedOfflineMapJob job = _offlineMapTask.DownloadPreplannedOfflineMap(parameters, path);

            // Set up event to update the progress bar while the job is in progress.
            job.ProgressChanged += OnJobProgressChanged;

            try
            {
                // Download the area.
                DownloadPreplannedOfflineMapResult results = await job.GetResultAsync();

                // Handle possible errors and show them to the user.
                if (results.HasErrors)
                {
                    // Accumulate all layer and table errors into a single message.
                    string errors = "";

                    foreach (KeyValuePair <Layer, Exception> layerError in results.LayerErrors)
                    {
                        errors = $"{errors}\n{layerError.Key.Name} {layerError.Value.Message}";
                    }

                    foreach (KeyValuePair <FeatureTable, Exception> tableError in results.TableErrors)
                    {
                        errors = $"{errors}\n{tableError.Key.TableName} {tableError.Value.Message}";
                    }

                    // Show the message.
                    await new MessageDialog(errors, "Warning!").ShowAsync();
                }

                // Show the downloaded map.
                MyMapView.Map = results.OfflineMap;

                // Update the UI.
                MessageLabel.Text = "Downloaded preplanned area.";
            }
            catch (Exception ex)
            {
                // Report any errors.
                Debug.WriteLine(ex);
                await new MessageDialog(ex.Message, "Downloading map area failed.").ShowAsync();
            }
            finally
            {
                BusyText.Text            = string.Empty;
                BusyIndicator.Visibility = Visibility.Collapsed;
            }
        }
        private async Task DownloadMapAreaAsync(PreplannedMapArea mapArea)
        {
            // Show the UI for the download.
            ProgressBar.Progress    = 0;
            BusyText.Text           = "Downloading map area...";
            BusyIndicator.IsVisible = true;
            MyMapView.IsVisible     = true;

            // Get the path for the downloaded map area.
            string path = Path.Combine(_offlineDataFolder, mapArea.PortalItem.Title);

            // If the area is already downloaded, open it and don't download it again.
            if (Directory.Exists(path))
            {
                MobileMapPackage localMapArea = await MobileMapPackage.OpenAsync(path);

                try
                {
                    // Load the map.
                    MyMapView.Map = localMapArea.Maps.First();

                    // Update the UI.
                    BusyText.Text           = string.Empty;
                    BusyIndicator.IsVisible = false;

                    // Return without proceeding to download.
                    return;
                }
                catch (Exception)
                {
                    // Do nothing, continue as if the map wasn't downloaded.
                }
            }

            // Create the job that is used to do the download.
            DownloadPreplannedOfflineMapJob job = _offlineMapTask.DownloadPreplannedOfflineMap(mapArea, path);

            // Subscribe to progress change events to support showing a progress bar.
            job.ProgressChanged += OnJobProgressChanged;

            try
            {
                // Download the map area.
                DownloadPreplannedOfflineMapResult results = await job.GetResultAsync();

                // Handle possible errors and show them to the user.
                if (results.HasErrors)
                {
                    StringBuilder errorBuilder = new StringBuilder();

                    // Add layer errors to the message.
                    foreach (KeyValuePair <Layer, Exception> layerError in results.LayerErrors)
                    {
                        errorBuilder.AppendLine($"{layerError.Key.Name} {layerError.Value.Message}");
                    }

                    // Add table errors to the message.
                    foreach (KeyValuePair <FeatureTable, Exception> tableError in results.TableErrors)
                    {
                        errorBuilder.AppendLine($"{tableError.Key.TableName} {tableError.Value.Message}");
                    }

                    // Show the message.
                    await((Page)Parent).DisplayAlert("Warning!", errorBuilder.ToString(), "OK");
                }

                // Show the Map in the MapView.
                MyMapView.Map = results.OfflineMap;
            }
            catch (Exception ex)
            {
                // Report the exception.
                await((Page)Parent).DisplayAlert("Downloading map area failed.", ex.Message, "OK");
            }
            finally
            {
                // Clear the loading UI.
                BusyText.Text           = string.Empty;
                BusyIndicator.IsVisible = false;
            }
        }
        private async void GeneratePreplannedMap()
        {
            ArcGISPortal portal = await ArcGISPortal.CreateAsync();

            string itemID = "a9081e4d02dc49f68c6202f03bfe302f";

            // Get a web map item using its ID.
            PortalItem webmapItem = await PortalItem.CreateAsync(portal, itemID);

            // Create a map from the web map item.
            Map onlineMap = new Map(webmapItem);

            // Create an OfflineMapTask from the map ...
            OfflineMapTask takeMapOfflineTask = await OfflineMapTask.CreateAsync(onlineMap);

            // ... or a web map portal item.
            //takeMapOfflineTask = await OfflineMapTask.CreateAsync(webmapItem);

            // Get a list of the available preplanned map areas.
            IReadOnlyList <PreplannedMapArea> preplannedMapAreaList = await takeMapOfflineTask.GetPreplannedMapAreasAsync();

            // Loop through all preplanned map areas.
            foreach (PreplannedMapArea mapArea in preplannedMapAreaList)
            {
                // Load the preplanned map area so property values can be read.
                await mapArea.LoadAsync();

                // Get the area of interest (geometry) for this area.
                Geometry aoi = mapArea.AreaOfInterest;

                // Get the portal item for this area, read the title and thumbnail image.
                PortalItem   preplannedMapItem = mapArea.PortalItem;
                string       mapTitle          = preplannedMapItem.Title;
                RuntimeImage areaThumbnail     = preplannedMapItem.Thumbnail;
            }


            PreplannedMapArea downloadMapArea = preplannedMapAreaList.First();

            string pathToOutputPackage = @"C:\LaurenCDrive\TL\Readiness\Trainings\RT_Offline_Workflows\PrePlannedMap";

            DownloadPreplannedOfflineMapJob preplannedMapJob = takeMapOfflineTask.DownloadPreplannedOfflineMap(downloadMapArea, pathToOutputPackage);

            // Generate the offline map and download it.
            DownloadPreplannedOfflineMapResult preplannedMapResult = await preplannedMapJob.GetResultAsync();

            if (!preplannedMapResult.HasErrors)
            {
                // Job completed successfully and all content was generated.
                Debug.WriteLine("Map " +
                                preplannedMapResult.MobileMapPackage.Item.Title +
                                " was saved to " +
                                preplannedMapResult.MobileMapPackage.Path);

                // Show the offline map in a MapView.
                MyMapView.Map = preplannedMapResult.OfflineMap;
            }
            else
            {
                // Job is finished but one or more layers or tables had errors.
                if (preplannedMapResult.LayerErrors.Count > 0)
                {
                    // Show layer errors.
                    foreach (var layerError in preplannedMapResult.LayerErrors)
                    {
                        Debug.WriteLine("Error occurred when taking " +
                                        layerError.Key.Name +
                                        " offline. Error : " +
                                        layerError.Value.Message);
                    }
                }
                if (preplannedMapResult.TableErrors.Count > 0)
                {
                    // Show table errors.
                    foreach (var tableError in preplannedMapResult.TableErrors)
                    {
                        Debug.WriteLine("Error occurred when taking " +
                                        tableError.Key.TableName +
                                        " offline. Error : " +
                                        tableError.Value.Message);
                    }
                }
            }
        }
        private async void DownloadMapAreaAsync(PreplannedMapArea mapArea)
        {
            // Close the current mobile package.
            _mobileMapPackage?.Close();

            // Set up UI for downloading.
            _activityIndicator.StartAnimating();
            _helpLabel.Text = "Downloading map area...";

            // Create folder path where the map package will be downloaded.
            string path = Path.Combine(_offlineDataFolder, mapArea.PortalItem.Title);

            // If the area is already downloaded, open it.
            if (Directory.Exists(path))
            {
                try
                {
                    _mobileMapPackage = await MobileMapPackage.OpenAsync(path);

                    _myMapView.Map = _mobileMapPackage.Maps.First();

                    _helpLabel.Text           = "Opened offline area.";
                    _showOnlineButton.Enabled = true;
                    _activityIndicator.StopAnimating();
                    return;
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    new UIAlertView("Couldn't open offline map area. Proceeding to take area offline.", e.ToString(), (IUIAlertViewDelegate)null, "OK", null).Show();
                }
            }

            // Create download parameters.
            DownloadPreplannedOfflineMapParameters parameters = await _offlineMapTask.CreateDefaultDownloadPreplannedOfflineMapParametersAsync(mapArea);

            // Set the update mode to not receive updates.
            parameters.UpdateMode = PreplannedUpdateMode.NoUpdates;

            // Create the job.
            DownloadPreplannedOfflineMapJob job = _offlineMapTask.DownloadPreplannedOfflineMap(parameters, path);

            // Set up event to update the progress bar while the job is in progress.
            job.ProgressChanged += Job_ProgressChanged;

            try
            {
                // Download the area.
                DownloadPreplannedOfflineMapResult results = await job.GetResultAsync();

                // Set the current mobile map package.
                _mobileMapPackage = results.MobileMapPackage;

                // Handle possible errors and show them to the user.
                if (results.HasErrors)
                {
                    // Accumulate all layer and table errors into a single message.
                    string errors = "";

                    foreach (KeyValuePair <Layer, Exception> layerError in results.LayerErrors)
                    {
                        errors = $"{errors}\n{layerError.Key.Name} {layerError.Value.Message}";
                    }

                    foreach (KeyValuePair <FeatureTable, Exception> tableError in results.TableErrors)
                    {
                        errors = $"{errors}\n{tableError.Key.TableName} {tableError.Value.Message}";
                    }

                    // Show the message.
                    new UIAlertView("Warning!", errors, (IUIAlertViewDelegate)null, "OK", null).Show();
                }

                // Show the downloaded map.
                _myMapView.Map = results.OfflineMap;
            }
            catch (Exception ex)
            {
                // Report any errors.
                Debug.WriteLine(ex);
                new UIAlertView("Downloading map area failed", ex.ToString(), (IUIAlertViewDelegate)null, "OK", null).Show();
            }
            finally
            {
                _activityIndicator.StopAnimating();
                _helpLabel.Text           = "Map area offline.";
                _showOnlineButton.Enabled = true;
            }
        }
        private async Task DownloadMapAreaAsync(PreplannedMapArea mapArea)
        {
            // Close the current mobile package.
            _mobileMapPackage?.Close();

            // Set up UI for downloading.
            ProgressView.Progress   = 0;
            BusyText.Text           = "Downloading map area...";
            BusyIndicator.IsVisible = true;

            // Create folder path where the map package will be downloaded.
            string path = Path.Combine(_offlineDataFolder, mapArea.PortalItem.Title);

            // If the area is already downloaded, open it.
            if (Directory.Exists(path))
            {
                try
                {
                    // Open the offline map package.
                    _mobileMapPackage = await MobileMapPackage.OpenAsync(path);

                    // Open the first map in the package.
                    MyMapView.Map = _mobileMapPackage.Maps.First();

                    // Update the UI.
                    BusyText.Text           = string.Empty;
                    BusyIndicator.IsVisible = false;
                    MessageLabel.Text       = "Opened offline area.";
                    return;
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    await Application.Current.MainPage.DisplayAlert("Couldn't open offline area. Proceeding to take area offline.", e.Message, "OK");
                }
            }

            // Create download parameters.
            DownloadPreplannedOfflineMapParameters parameters = await _offlineMapTask.CreateDefaultDownloadPreplannedOfflineMapParametersAsync(mapArea);

            // Set the update mode to not receive updates.
            parameters.UpdateMode = PreplannedUpdateMode.NoUpdates;

            // Create the job.
            DownloadPreplannedOfflineMapJob job = _offlineMapTask.DownloadPreplannedOfflineMap(parameters, path);

            // Set up event to update the progress bar while the job is in progress.
            job.ProgressChanged += OnJobProgressChanged;

            try
            {
                // Download the area.
                DownloadPreplannedOfflineMapResult results = await job.GetResultAsync();

                // Set the current mobile map package.
                _mobileMapPackage = results.MobileMapPackage;

                // Handle possible errors and show them to the user.
                if (results.HasErrors)
                {
                    // Accumulate all layer and table errors into a single message.
                    string errors = "";

                    foreach (KeyValuePair <Layer, Exception> layerError in results.LayerErrors)
                    {
                        errors = $"{errors}\n{layerError.Key.Name} {layerError.Value.Message}";
                    }

                    foreach (KeyValuePair <FeatureTable, Exception> tableError in results.TableErrors)
                    {
                        errors = $"{errors}\n{tableError.Key.TableName} {tableError.Value.Message}";
                    }

                    // Show the message.
                    await Application.Current.MainPage.DisplayAlert("Warning!", errors, "OK");
                }

                // Show the downloaded map.
                MyMapView.Map = results.OfflineMap;

                // Update the UI.
                ShowOnlineButton.IsEnabled = true;
                MessageLabel.Text          = "Downloaded preplanned area.";
                DownloadButton.Text        = "Display";
            }
            catch (Exception ex)
            {
                // Report any errors.
                Debug.WriteLine(ex);
                await Application.Current.MainPage.DisplayAlert("Downloading map area failed.", ex.Message, "OK");
            }
            finally
            {
                BusyText.Text           = string.Empty;
                BusyIndicator.IsVisible = false;
            }
        }