private async void TakeMapOfflineButton_Click(object sender, EventArgs e) { // Create a path for the output mobile map. string tempPath = $"{Path.GetTempPath()}"; string[] outputFolders = Directory.GetDirectories(tempPath, "NapervilleWaterNetwork*"); // Loop through the folder names and delete them. foreach (string dir in outputFolders) { try { // Delete the folder. Directory.Delete(dir, true); } catch (Exception) { // Ignore exceptions (files might be locked, for example). } } // Create a new folder for the output mobile map. _packagePath = Path.Combine(tempPath, @"NapervilleWaterNetwork"); int num = 1; while (Directory.Exists(_packagePath)) { _packagePath = Path.Combine(tempPath, @"NapervilleWaterNetwork" + num.ToString()); num++; } // Create the output directory. Directory.CreateDirectory(_packagePath); try { // Create an offline map task with the current (online) map. _takeMapOfflineTask = await OfflineMapTask.CreateAsync(_mapView.Map); // Create the default parameters for the task, pass in the area of interest. _parameters = await _takeMapOfflineTask.CreateDefaultGenerateOfflineMapParametersAsync(_areaOfInterest); // Get the overrides. _overrides = await _takeMapOfflineTask.CreateGenerateOfflineMapParameterOverridesAsync(_parameters); // Create the overrides UI. ParameterOverrideFragment overlayFragment = new ParameterOverrideFragment(_overrides, _mapView.Map); // Complete configuration when the dialog is closed. overlayFragment.FinishedConfiguring += ConfigurationContinuation; // Display the configuration window. overlayFragment.Show(FragmentManager, ""); } catch (Exception ex) { // Exception while taking the map offline. ShowStatusMessage(ex.Message); } }
private async void TakeMapOfflineButton_Click(object sender, EventArgs e) { // Create a path for the output mobile map. string tempPath = $"{Path.GetTempPath()}"; string[] outputFolders = Directory.GetDirectories(tempPath, "NapervilleWaterNetwork*"); // Loop through the folder names and delete them. foreach (string dir in outputFolders) { try { // Delete the folder. Directory.Delete(dir, true); } catch (Exception) { // Ignore exceptions (files might be locked, for example). } } // Create a new folder for the output mobile map. string packagePath = Path.Combine(tempPath, @"NapervilleWaterNetwork"); int num = 1; while (Directory.Exists(packagePath)) { packagePath = Path.Combine(tempPath, @"NapervilleWaterNetwork" + num.ToString()); num++; } // Create the output directory. Directory.CreateDirectory(packagePath); try { // Create an offline map task with the current (online) map. OfflineMapTask takeMapOfflineTask = await OfflineMapTask.CreateAsync(_mapView.Map); // Create the default parameters for the task, pass in the area of interest. GenerateOfflineMapParameters parameters = await takeMapOfflineTask.CreateDefaultGenerateOfflineMapParametersAsync(_areaOfInterest); // Get the overrides. GenerateOfflineMapParameterOverrides overrides = await takeMapOfflineTask.CreateGenerateOfflineMapParameterOverridesAsync(parameters); // Create the overrides UI. ParameterOverrideFragment overlayFragment = new ParameterOverrideFragment(overrides, _mapView.Map); // Resume work when the dialog is closed. overlayFragment.FinishedConfiguring += async(o, args) => { // Show the progress dialog while the job is running. _alertDialog.Show(); // Create the job with the parameters and output location. _generateOfflineMapJob = takeMapOfflineTask.GenerateOfflineMap(parameters, packagePath, overrides); // Handle the progress changed event for the job. _generateOfflineMapJob.ProgressChanged += OfflineMapJob_ProgressChanged; // Await the job to generate geodatabases, export tile packages, and create the mobile map package. GenerateOfflineMapResult results = await _generateOfflineMapJob.GetResultAsync(); // Check for job failure (writing the output was denied, e.g.). if (_generateOfflineMapJob.Status != JobStatus.Succeeded) { // Report failure to the user. ShowStatusMessage("Failed to take the map offline."); } // Check for errors with individual layers. if (results.LayerErrors.Any()) { // Build a string to show all layer errors. System.Text.StringBuilder errorBuilder = new System.Text.StringBuilder(); foreach (KeyValuePair <Layer, Exception> layerError in results.LayerErrors) { errorBuilder.AppendLine($"{layerError.Key.Id} : {layerError.Value.Message}"); } // Show layer errors. ShowStatusMessage(errorBuilder.ToString()); } // Display the offline map. _mapView.Map = results.OfflineMap; // Apply the original viewpoint for the offline map. _mapView.SetViewpoint(new Viewpoint(_areaOfInterest)); // Enable map interaction so the user can explore the offline data. _mapView.InteractionOptions.IsEnabled = true; // Change the title and disable the "Take map offline" button. _takeMapOfflineButton.Text = "Map is offline"; _takeMapOfflineButton.Enabled = false; }; overlayFragment.Show(FragmentManager, ""); } catch (TaskCanceledException) { // Generate offline map task was canceled. ShowStatusMessage("Taking map offline was canceled"); } catch (Exception ex) { // Exception while taking the map offline. ShowStatusMessage(ex.Message); } finally { // Hide the loading overlay when the job is done. _alertDialog.Dismiss(); } }