private async void _myDrawObject_DrawComplete(object sender, DrawEventArgs e) { if (e.Geometry == null) { MapApplication.Current.HideWindow(_elevationProfileDialog); return; } try { _myDrawObject.IsEnabled = false; ProfileCalculationBusyIndicator.Visibility = Visibility.Visible; _lineGraphicLayer.Graphics[0].Geometry = e.Geometry; if (_cts != null) _cts.Cancel(); _cts = new CancellationTokenSource(); Geoprocessor geoprocessorTask = new Geoprocessor( "http://elevation.arcgis.com/arcgis/rest/services/Tools/ElevationSync/GPServer/Profile"); List<GPParameter> parameters = new List<GPParameter>(); parameters.Add(new GPFeatureRecordSetLayer("InputLineFeatures", e.Geometry)); parameters.Add(new GPString("returnM", "true")); parameters.Add(new GPString("returnZ", "true")); GPExecuteResults results = await geoprocessorTask.ExecuteTaskAsync(parameters, _cts.Token); if (results == null || results.OutParameters.Count == 0 || (results.OutParameters[0] as GPFeatureRecordSetLayer).FeatureSet.Features.Count == 0) { MessageBox.Show("Fail to get elevation data. Draw another line"); return; } ESRI.ArcGIS.Client.Geometry.Polyline elevationLine = (results.OutParameters[0] as GPFeatureRecordSetLayer).FeatureSet.Features[0].Geometry as ESRI.ArcGIS.Client.Geometry.Polyline; foreach (MapPoint p in elevationLine.Paths[0]) { p.M = Math.Round(p.M / 1000, 2); p.Z = Math.Round(p.Z, 2); } MapPoint lastPoint = elevationLine.Paths[0][elevationLine.Paths[0].Count - 1]; _elevationProfileDialog.ProfileDistance = lastPoint.M; _elevationProfileDialog.ProfileData = elevationLine.Paths[0]; var top = ((FrameworkElement)Application.Current.RootVisual).ActualHeight - 400; if (MapApplication.Current.IsEditMode) top -= 20; MapApplication.Current.ShowWindow("", _elevationProfileDialog, false, (s, a) => ClearLayers(), null, WindowType.Floating, top); } catch (Exception ex) { if (ex is ServiceException) { MessageBox.Show(String.Format("{0}: {1}", (ex as ServiceException).Code.ToString(), (ex as ServiceException).Details[0]), "Error", MessageBoxButton.OK); return; } } finally { ProfileCalculationBusyIndicator.Visibility = Visibility.Collapsed; } }
async void _myDrawObject_DrawComplete(object sender, DrawEventArgs e) { if (e.Geometry == null) { ChartContainer.Visibility = System.Windows.Visibility.Collapsed; return; } try { _myDrawObject.IsEnabled = false; this.Cursor = Cursors.Wait; _lineGraphicLayer.Graphics[0].Geometry = e.Geometry; if (_cts != null) _cts.Cancel(); _cts = new CancellationTokenSource(); Geoprocessor geoprocessorTask = new Geoprocessor( "http://elevation.arcgis.com/arcgis/rest/services/Tools/ElevationSync/GPServer/Profile"); List<GPParameter> parameters = new List<GPParameter>(); parameters.Add(new GPFeatureRecordSetLayer("InputLineFeatures", e.Geometry)); parameters.Add(new GPString("returnM", "true")); parameters.Add(new GPString("returnZ", "true")); GPExecuteResults results = await geoprocessorTask.ExecuteTaskAsync(parameters, _cts.Token); if (results == null || results.OutParameters.Count == 0 || (results.OutParameters[0] as GPFeatureRecordSetLayer).FeatureSet.Features.Count == 0) { MessageBox.Show("Fail to get elevation data. Draw another line"); return; } ESRI.ArcGIS.Client.Geometry.Polyline elevationLine = (results.OutParameters[0] as GPFeatureRecordSetLayer).FeatureSet.Features[0].Geometry as ESRI.ArcGIS.Client.Geometry.Polyline; foreach (MapPoint p in elevationLine.Paths[0]) { p.M = Math.Round(p.M / 1000, 2); p.Z = Math.Round(p.Z, 2); } MapPoint lastPoint = elevationLine.Paths[0][elevationLine.Paths[0].Count - 1]; lblDistance.Text = string.Format("Total Distance {0} Kilometers", lastPoint.M.ToString()); (ElevationChart.Series[0] as LineSeries).ItemsSource = elevationLine.Paths[0]; ChartContainer.Visibility = System.Windows.Visibility.Visible; } catch (Exception ex) { if (ex is ServiceException) { MessageBox.Show(String.Format("{0}: {1}", (ex as ServiceException).Code.ToString(), (ex as ServiceException).Details[0]), "Error", MessageBoxButton.OK); return; } } finally { this.Cursor = Cursors.Arrow; } }