private async void OnAnalyzeHotspotsClicked(object sender, RoutedEventArgs e)
{
// Show the busyOverlay indication
ShowBusyOverlay();
// Get the 'from' and 'to' dates from the date pickers for the geoprocessing analysis
var myFromDate = FromDate.SelectedDate.Value;
var myToDate = ToDate.SelectedDate.Value;
// The end date must be at least one day after the start date
if (myToDate <= myFromDate.AddDays(1))
{
// Show error message
MessageBox.Show(
"Please select valid time range. There has to be at least one day in between To and From dates.",
"Invalid date range");
// Remove the busyOverlay
ShowBusyOverlay(false);
return;
}
// Create the parameters that are passed to the used geoprocessing task
GeoprocessingParameters myHotspotParameters = new GeoprocessingParameters(GeoprocessingExecutionType.AsynchronousSubmit);
// Construct the date query
var myQueryString = string.Format("(\"DATE\" > date '{0} 00:00:00' AND \"DATE\" < date '{1} 00:00:00')",
myFromDate.ToString("yyyy-MM-dd"),
myToDate.ToString("yyyy-MM-dd"));
// Add the query that contains the date range used in the analysis
myHotspotParameters.Inputs.Add("Query", new GeoprocessingString(myQueryString));
// Create job that handles the communication between the application and the geoprocessing task
_hotspotJob = _hotspotTask.CreateJob(myHotspotParameters);
try
{
// Execute the geoprocessing analysis and wait for the results
GeoprocessingResult myAnalysisResult = await _hotspotJob.GetResultAsync();
// Add results to a map using map server from a geoprocessing task
// Load to get access to full extent
await myAnalysisResult.MapImageLayer.LoadAsync();
// Add the analysis layer to the map view
MyMapView.Map.OperationalLayers.Add(myAnalysisResult.MapImageLayer);
// Zoom to the results
await MyMapView.SetViewpointAsync(new Viewpoint(myAnalysisResult.MapImageLayer.FullExtent));
}
catch (TaskCanceledException)
{
// This is thrown if the task is canceled. Ignore.
}
catch (Exception ex)
{
// Display error messages if the geoprocessing task fails
if (_hotspotJob.Status == JobStatus.Failed && _hotspotJob.Error != null)
MessageBox.Show("Executing geoprocessing failed. " + _hotspotJob.Error.Message, "Geoprocessing error");
else
MessageBox.Show("An error occurred. " + ex.ToString(), "Sample error");
}
finally
{
// Remove the busyOverlay
ShowBusyOverlay(false);
}
}