private async void TakeMapOfflineButton_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) { // Create a new folder for the output mobile map. string packagePath = Path.Combine(Environment.ExpandEnvironmentVariables("%TEMP%"), @"NapervilleWaterNetwork"); int num = 1; while (Directory.Exists(packagePath)) { packagePath = Path.Combine(Environment.ExpandEnvironmentVariables("%TEMP%"), @"NapervilleWaterNetwork" + num.ToString()); num++; } // Create the output directory. Directory.CreateDirectory(packagePath); try { // Show the progress indicator while the job is running. BusyIndicator.Visibility = Microsoft.UI.Xaml.Visibility.Visible; // Create an offline map task with the current (online) map. OfflineMapTask takeMapOfflineTask = await OfflineMapTask.CreateAsync(MyMapView.Map); // Create the default parameters for the task, pass in the area of interest. GenerateOfflineMapParameters parameters = await takeMapOfflineTask.CreateDefaultGenerateOfflineMapParametersAsync(_areaOfInterest); #region overrides // Generate parameter overrides for more in-depth control of the job. GenerateOfflineMapParameterOverrides overrides = await takeMapOfflineTask.CreateGenerateOfflineMapParameterOverridesAsync(parameters); // Configure the overrides using helper methods. ConfigureTileLayerOverrides(overrides); ConfigureLayerExclusion(overrides); CropWaterPipes(overrides); ApplyFeatureFilter(overrides); // Create the job with the parameters and output location. _generateOfflineMapJob = takeMapOfflineTask.GenerateOfflineMap(parameters, packagePath, overrides); #endregion 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) { var dialog = new MessageDialog2("Generate offline map package failed.", "Job status"); await dialog.ShowAsync(); BusyIndicator.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed; } // 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(string.Format("{0} : {1}", layerError.Key.Id, layerError.Value.Message)); } // Show layer errors. string errorText = errorBuilder.ToString(); var dialog = new MessageDialog2(errorText, "Layer errors"); await dialog.ShowAsync(); } // Display the offline map. MyMapView.Map = results.OfflineMap; // Apply the original viewpoint for the offline map. MyMapView.SetViewpoint(new Viewpoint(_areaOfInterest)); // Enable map interaction so the user can explore the offline data. MyMapView.InteractionOptions.IsEnabled = true; // Hide the "Take map offline" button. TakeOfflineArea.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed; // Show a message that the map is offline. MessageArea.Visibility = Microsoft.UI.Xaml.Visibility.Visible; } catch (TaskCanceledException) { // Generate offline map task was canceled. var dialog = new MessageDialog2("Taking map offline was canceled"); await dialog.ShowAsync(); } catch (Exception ex) { // Exception while taking the map offline. var dialog = new MessageDialog2(ex.Message, "Offline map error"); await dialog.ShowAsync(); } finally { // Hide the activity indicator when the job is done. BusyIndicator.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed; } }
private async void TakeMapOfflineButton_Click(object sender, EventArgs e) { // Clean up any previous outputs in the temp directory. 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 { // Show the progress indicator while the job is running. busyIndicator.IsVisible = true; // Create an offline map task with the current (online) map. OfflineMapTask takeMapOfflineTask = await OfflineMapTask.CreateAsync(MyMapView.Map); // Create the default parameters for the task, pass in the area of interest. GenerateOfflineMapParameters parameters = await takeMapOfflineTask.CreateDefaultGenerateOfflineMapParametersAsync(_areaOfInterest); // Generate parameter overrides for more in-depth control of the job. GenerateOfflineMapParameterOverrides overrides = await takeMapOfflineTask.CreateGenerateOfflineMapParameterOverridesAsync(parameters); // Create the UI for configuring the overrides. ConfigureOverridesPage configurationPage = new ConfigureOverridesPage(overrides, MyMapView.Map); // Wait for the user to finish with the configuration, then continue. configurationPage.Disappearing += async(o, args) => { // Re-show the busy indicator. busyIndicator.IsVisible = true; // 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) { await Application.Current.MainPage.DisplayAlert("Alert", "Generate offline map package failed.", "OK"); busyIndicator.IsVisible = false; } // 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. string errorText = errorBuilder.ToString(); await Application.Current.MainPage.DisplayAlert("Alert", errorText, "OK"); } // Display the offline map. MyMapView.Map = results.OfflineMap; // Apply the original viewpoint for the offline map. MyMapView.SetViewpoint(new Viewpoint(_areaOfInterest)); // Enable map interaction so the user can explore the offline data. MyMapView.InteractionOptions.IsEnabled = true; // Hide the "Take map offline" button. takeOfflineArea.IsVisible = false; // Show a message that the map is offline. messageArea.IsVisible = true; // Hide the busy indicator. busyIndicator.IsVisible = false; }; // Show the configuration UI. await Navigation.PushModalAsync(configurationPage, true); } catch (TaskCanceledException) { // Generate offline map task was canceled. await Application.Current.MainPage.DisplayAlert("Alert", "Taking map offline was canceled", "OK"); } catch (Exception ex) { // Exception while taking the map offline. await Application.Current.MainPage.DisplayAlert("Alert", ex.Message, "OK"); } finally { // Hide the activity indicator when the job is done. busyIndicator.IsVisible = false; } }
private async void TakeMapOfflineButton_Click(object sender, EventArgs e) { // Make sure the user is logged in. /* * bool loggedIn = await EnsureLoggedInAsync(); * if (!loggedIn) * { * return; * } */ // Disable the button to prevent errors. _takeMapOfflineButton.Enabled = false; // Show the loading indicator. _loadingIndicator.StartAnimating(); // 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 ex) { // Ignore exceptions (files might be locked, for example). Debug.WriteLine(ex); } } // 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); num++; } // Create the output directory. Directory.CreateDirectory(_packagePath); try { // Show the loading overlay while the job is running. _statusLabel.Text = "Taking map offline..."; // Create an offline map task with the current (online) map. _takeMapOfflineTask = await OfflineMapTask.CreateAsync(_myMapView.Map); // Create the default parameters for the task, pass in the area of interest. _parameters = await _takeMapOfflineTask.CreateDefaultGenerateOfflineMapParametersAsync(_areaOfInterest); // Generate parameter overrides for more in-depth control of the job. _overrides = await _takeMapOfflineTask.CreateGenerateOfflineMapParameterOverridesAsync(_parameters); // Show the configuration window. ShowConfigurationWindow(_overrides); // Finish work once the user has configured the override. _overridesVC.FinishedConfiguring += ConfigurationContinuation; } catch (TaskCanceledException) { // Generate offline map task was canceled. UIAlertController messageAlert = UIAlertController.Create("Canceled", "Taking map offline was canceled", UIAlertControllerStyle.Alert); messageAlert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null)); PresentViewController(messageAlert, true, null); } catch (Exception ex) { // Exception while taking the map offline. UIAlertController messageAlert = UIAlertController.Create("Error", ex.Message, UIAlertControllerStyle.Alert); messageAlert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null)); PresentViewController(messageAlert, true, null); } finally { // Hide the loading overlay when the job is done. _loadingIndicator.StopAnimating(); } }