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; } }
private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e) { IsBusy = true; // Cancel any outstanding Tasks _gpTask.CancelAsync(); // Get the GraphicsLayer GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer; graphicsLayer.ClearGraphics(); e.MapPoint.SpatialReference = MyMap.SpatialReference; // Add a graphic at the click point Graphic graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = e.MapPoint, Symbol = LayoutRoot.Resources["DefaultClickSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol }; graphic.SetZIndex(1); graphicsLayer.Graphics.Add(graphic); // Create the graphic to submit. Graphic g = new Graphic() { Geometry = _mercator.ToGeographic(e.MapPoint) }; // Create a new list of GP Parameters List <GPParameter> gpParams = new List <GPParameter>(); // We want to specify input attributes - create a new FeatureSet. FeatureSet featureSet = new FeatureSet(); // Create the Fields and add one called "VALUE". featureSet.Fields = new List <Field> { new Field() { FieldName = "VALUE", Type = Field.FieldType.String, Alias = "VALUE" } }; //var fs = new FeatureSet(new List<Graphic> { g }); // Add the graphic to the FeatureSet featureSet.Features.Add(g); // Set the graphic's attribute featureSet.Features[0].Attributes["VALUE"] = valueText.Text; // Add the GP Paramr gpParams.Add(new GPFeatureRecordSetLayer("InputFeatures", featureSet)); gpParams.Add(new GPLinearUnit("Distance", esriUnits.esriKilometers, 500)); // Register an inline handler for ExecuteCompleted event _gpTask.ExecuteCompleted += (s, e1) => { // Clear the graphics layer (will remove the input Pushpin) graphicsLayer.Graphics.Clear(); // Get the results GPExecuteResults results = e1.Results; GPFeatureRecordSetLayer rs = results.OutParameters[0] as GPFeatureRecordSetLayer; // Get the result feature Graphic graphicBuff = rs.FeatureSet.Features[0]; // Set the symbol graphicBuff.Symbol = LayoutRoot.Resources["DefaultFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol; // Add to the layer graphicsLayer.Graphics.Add(graphicBuff); // Stop the progress bar IsBusy = false; }; // Register an inline handler for the failed event (just in case) _gpTask.Failed += (s2, e2) => { MessageBox.Show(e2.Error.Message); IsBusy = false; }; // Execute the Buffer asynchronously _gpTask.ExecuteAsync(gpParams); }