private async void Initialize() { try { // Close the current mobile package when the sample closes. Unloaded += (s, e) => { _mobileMapPackage?.Close(); }; // The data manager provides a method to get a suitable offline data folder. _offlineDataFolder = Path.Combine(DataManager.GetDataFolder(), "SampleData", "DownloadPreplannedMapAreas"); // If temporary data folder doesn't exists, create it. if (!Directory.Exists(_offlineDataFolder)) { Directory.CreateDirectory(_offlineDataFolder); } // Create a portal to enable access to the portal item. ArcGISPortal portal = await ArcGISPortal.CreateAsync(); // Create the portal item from the portal item ID. PortalItem webMapItem = await PortalItem.CreateAsync(portal, PortalItemId); // Show the map. _originalMap = new Map(webMapItem); MyMapView.Map = _originalMap; // Create an offline map task for the web map item. _offlineMapTask = await OfflineMapTask.CreateAsync(webMapItem); // Find the available preplanned map areas. IReadOnlyList <PreplannedMapArea> preplannedAreas = await _offlineMapTask.GetPreplannedMapAreasAsync(); // Load each item, then add it to the UI. foreach (PreplannedMapArea area in preplannedAreas) { await area.LoadAsync(); AreasList.Items.Add(area); } // Hide the loading indicator. BusyIndicator.Visibility = Visibility.Collapsed; } catch (Exception ex) { // Something unexpected happened, show the error message. Debug.WriteLine(ex); await new MessageDialog(ex.Message, "There was an error.").ShowAsync(); } }
protected override void OnStop() { base.OnStop(); // Close the mobile map package when the sample closes. _mobileMapPackage?.Close(); }
private async void ApplyUpdates() { 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; // Set the map package to rollback to the old state should the sync job fail. parameters.RollbackOnFailure = true; // 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(); 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 Application.Current.MainPage.DisplayAlert("Error", "Failed to load the mobile map package.", "OK"); } } // Verify that the map is up to date and change the UI to reflect the update availability status. CheckForScheduledUpdates(); } else { await Application.Current.MainPage.DisplayAlert("Error", "Error syncing the offline map.", "OK"); } } catch (Exception ex) { await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "OK"); } }
private async void ApplyUpdatesClicked(object sender, EventArgs 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 { new UIAlertView("Error", "Failed to load the mobile map package.", (IUIAlertViewDelegate)null, "OK", null).Show(); } } // Verify that the map is up to date and change the UI to reflect the update availability status. CheckForScheduledUpdates(); } else { new UIAlertView("Error", "Error syncing the offline map.", (IUIAlertViewDelegate)null, "OK", null).Show(); } } catch (Exception ex) { new UIAlertView("Error", ex.Message, (IUIAlertViewDelegate)null, "OK", null).Show(); } }
public override void ViewDidDisappear(bool animated) { base.ViewDidDisappear(animated); // Close the mobile map package when the sample closes. _mobileMapPackage?.Close(); // Unsubscribe from events, per best practice. _applyButton.Clicked -= ApplyUpdatesClicked; }
public override void ViewDidDisappear(bool animated) { base.ViewDidDisappear(animated); // Unsubscribe from events, per best practice. _applyButton.Clicked -= ApplyUpdatesClicked; // Check if sample is being closed. if (NavigationController?.ViewControllers == null) { // Close the mobile map package when the sample closes. _mobileMapPackage?.Close(); } }
private async void 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 MessageDialog(ex.Message, "Error").ShowAsync(); } }
private async void ActivateButton_Clicked(object sender, EventArgs e) { // Open the map package. MobileMapPackage myMapPackage = await MobileMapPackage.OpenAsync(GetMmpkPath()); await myMapPackage.LoadAsync(); // Get our annotation layer var annoLayer = myMapPackage.Maps.First().AllLayers.First(); myMapPackage.Maps.First().OperationalLayers.Clear(); myMapPackage.Close(); // Add it to our offline map area _offlineMapArea.OperationalLayers.Add(annoLayer); TheMap.Map = _offlineMapArea; Device.BeginInvokeOnMainThread(() => { ActivateMapAreaButton.IsEnabled = false; UpdateFeatureButton.IsEnabled = true; }); }
public void Dispose() { // Close the mobile map package when the sample closes. _mobileMapPackage?.Close(); }
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; } }
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; } }