private async void SaveMapClicked(object sender, RoutedEventArgs e)
{
try
{
// Show the progress bar so the user knows work is happening
SaveProgressBar.Visibility = Visibility.Visible;
// Get the current map
var myMap = MyMapView.Map;
// Apply the current extent as the map's initial extent
myMap.InitialViewpoint = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry);
// See if the map has already been saved (has an associated portal item)
if (myMap.Item == null)
{
// Get information for the new portal item
var title = TitleTextBox.Text;
var description = DescriptionTextBox.Text;
var tags = TagsTextBox.Text.Split(',');
// Make sure all required info was entered
if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(description) || tags.Length == 0)
{
throw new Exception("Please enter a title, description, and some tags to describe the map.");
}
// Call a function to save the map as a new portal item
await SaveNewMapAsync(MyMapView.Map, title, description, tags);
// Report a successful save
MessageBox.Show("Saved '" + title + "' to ArcGIS Online!", "Map Saved");
}
else
{
// This is not the initial save, call SaveAsync to save changes to the existing portal item
await myMap.SaveAsync();
// Report update was successful
MessageBox.Show("Saved changes to '" + myMap.Item.Title + "'", "Updates Saved");
}
// Update the portal item thumbnail with the current map image
try
{
// Export the current map view
var mapImage = await Esri.ArcGISRuntime.UI.RuntimeImageExtensions.ToImageSourceAsync(await MyMapView.ExportImageAsync());
// Call a function that writes a temporary jpeg file of the map
var imagePath = await WriteTempThumbnailImageAsync(mapImage);
// Call a function to update the portal item's thumbnail with the image
UpdatePortalItemThumbnailAsync(imagePath);
}
catch
{
// Throw an exception to let the user know the thumbnail was not saved (the map item was)
throw new Exception("Thumbnail was not updated.");
}
}
catch (Exception ex)
{
// Report error message
MessageBox.Show("Error saving map to ArcGIS Online: " + ex.Message);
}
finally
{
// Hide the progress bar
SaveProgressBar.Visibility = Visibility.Hidden;
}
}
#endregion