Click or drag to resize

SketchEditor Class

Sketch editor used for performing edits on a map.
Inheritance Hierarchy
SystemObject
  Esri.ArcGISRuntime.UISketchEditor

Namespace:  Esri.ArcGISRuntime.UI
Assembly:  Esri.ArcGISRuntime (in Esri.ArcGISRuntime.dll) Version: 100.11.0
Syntax
public sealed class SketchEditor : INotifyPropertyChanged, 
	IGeometryEditor

The SketchEditor type exposes the following members.

Constructors
  NameDescription
Public methodSketchEditor
Initializes a new instance of the SketchEditor class.
Top
Properties
  NameDescription
Public propertyAddCommand
Method invoked to add vertex to the end of a multipart geometry Polyline or Polygon where the command parameter contains the MapPoint (or vertex) to add.
Public propertyCode exampleCancelCommand
Cancels the current draw or edit operation.
Public propertyCode exampleCompleteCommand
Method invoked to force commit the draw or geometry edit.
Public propertyCreationMode
Gets the creation mode.
Public propertyDeleteCommand
Method invoked to remove vertex from a multipart geometry Polyline or Polygon Use the CommandParameter to specify the coordinate index of vertex to be removed. Otherwise, select vertex by PointerPress or MouseDown to enable delete on selected vertex. Note also that in order to remove vertex, the multipart geometry must contain its minimum number of vertices required to make it a valid geometry. For example, Polyline must at least have 2 vertices and Polygon must at least have 3 vertices.
Public propertyEditConfiguration
Gets the EditConfiguration that defines capabilities used by the SketchEditor
Public propertyGeometry
Gets the current geometry.
Public propertyIsEnabled
Gets or sets a value indicating if SketchEditor is enabled to respond to map interaction.
Public propertyIsVisible
Gets or sets a value indicating if SketchEditor graphics are visible.
Public propertyOpacity
Gets or sets the opacity applied to SketchEditor graphics. Must be a value between 0 and 1.
Public propertyRedoCommand
Method invoked to redo the last canceled geometry edit action.
Public propertySelectedVertex
Gets information on the current selected vertex.
Public propertyStyle
Gets or sets the EditConfiguration that defines the symbology used by the SketchEditor
Public propertyUndoCommand
Method invoked to undo the last geometry edit action.
Top
Methods
  NameDescription
Public methodClearGeometry
Clears the current geometry.
Public methodClearVertexSelection
Clears selection on vertices.
Public methodInsertVertexAfterSelectedVertex
Returns a value indicating whether a new vertex with the specified location has been inserted after selected vertex.
Public methodMoveSelectedVertex
Returns a value indicating whether the selected vertex has been moved to the specified new location.
Public methodRemoveSelectedVertex
Returns a value indicating whether the selected vertex has been removed.
Public methodReplaceGeometry
Replaces current geometry with specified geometry.
Public methodSelectMidVertex
Selects mid-vertex with specified indices
Public methodSelectVertex
Selects vertex with specified indices
Public methodCode exampleStartAsync(Geometry)
Starts to edit Geometry based on the SketchEditConfiguration that the geometry supports.
Public methodStartAsync(Geometry, SketchCreationMode)
Starts to edit Geometry based on specified SketchCreationMode with the SketchEditConfiguration that the geometry supports.
Public methodStartAsync(SketchCreationMode, SketchEditConfiguration)
Starts to draw geometry based on specified SketchCreationMode and proceeds to edit geometry with the specified SketchEditConfiguration.
Public methodCode exampleStartAsync(SketchCreationMode, Boolean)
Starts to draw geometry based on specified SketchCreationMode and proceeds to edit geometry with the SketchEditConfiguration that the geometry supports; unless, optional drawAndEdit parameter is false, then sketch is complete once geometry is drawn.
Public methodStartAsync(Geometry, SketchCreationMode, SketchEditConfiguration)
Starts to edit Geometry based on specified SketchCreationMode and SketchEditConfiguration.
Public methodStop
Stops the current sketch, which means Geometry is cleared along with its undo stack. There is no further updates by interaction nor programmatic methods that can update geometry.
Top
Events
  NameDescription
Public eventGeometryChanged
Occurs when the geometry has been moved, resized, rotated, or updated.
Public eventPropertyChanged
Occurs when a non-dependency property value changes.
Public eventSelectedVertexChanged
Occurs when the selected vertex has changed.
Top
Remarks

A sketch editor can only be associated with one map instance.

Examples

WPF

Example Name: SketchOnMap

Use the Sketch Editor to edit or sketch a new point, line, or polygon geometry on to a map.

Code example screen shot.

C#
// Copyright 2017 Esri.
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an 
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific 
// language governing permissions and limitations under the License.

using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Drawing;
using Point = System.Windows.Point;

namespace ArcGISRuntime.WPF.Samples.SketchOnMap
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        name: "Sketch on map",
        category: "GraphicsOverlay",
        description: "Use the Sketch Editor to edit or sketch a new point, line, or polygon geometry on to a map.",
        instructions: "Choose which geometry type to sketch from one of the available buttons. Choose from points, multipoints, polylines, polygons, freehand polylines, and freehand polygons.",
        tags: new[] { "draw", "edit" })]
    public partial class SketchOnMap
    {
        // Graphics overlay to host sketch graphics
        private GraphicsOverlay _sketchOverlay;

        public SketchOnMap()
        {
            InitializeComponent();

            // Call a function to set up the map and sketch editor
            Initialize();
        }

        private void Initialize()
        {
            // Create a light gray canvas map
            Map myMap = new Map(BasemapStyle.ArcGISLightGray);

            // Create graphics overlay to display sketch geometry
            _sketchOverlay = new GraphicsOverlay();
            MyMapView.GraphicsOverlays.Add(_sketchOverlay);

            // Assign the map to the MapView
            MyMapView.Map = myMap;

            // Fill the combo box with choices for the sketch modes (shapes)
            SketchModeComboBox.ItemsSource = System.Enum.GetValues(typeof(SketchCreationMode));
            SketchModeComboBox.SelectedIndex = 0;

            // Set the sketch editor as the page's data context
            DataContext = MyMapView.SketchEditor;
        }

        #region Graphic and symbol helpers
        private Graphic CreateGraphic(Esri.ArcGISRuntime.Geometry.Geometry geometry)
        {
            // Create a graphic to display the specified geometry
            Symbol symbol = null;
            switch (geometry.GeometryType)
            {
                // Symbolize with a fill symbol
                case GeometryType.Envelope:
                case GeometryType.Polygon:
                    {
                        symbol = new SimpleFillSymbol()
                        {
                            Color = Color.Red,
                            Style = SimpleFillSymbolStyle.Solid
                        };
                        break;
                    }
                // Symbolize with a line symbol
                case GeometryType.Polyline:
                    {
                        symbol = new SimpleLineSymbol()
                        {
                            Color = Color.Red,
                            Style = SimpleLineSymbolStyle.Solid,
                            Width = 5d
                        };
                        break;
                    }
                // Symbolize with a marker symbol
                case GeometryType.Point:
                case GeometryType.Multipoint:
                    {

                        symbol = new SimpleMarkerSymbol()
                        {
                            Color = Color.Red,
                            Style = SimpleMarkerSymbolStyle.Circle,
                            Size = 15d
                        };
                        break;
                    }
            }

            // pass back a new graphic with the appropriate symbol
            return new Graphic(geometry, symbol);
        }

        private async Task<Graphic> GetGraphicAsync()
        {
            // Wait for the user to click a location on the map
            Geometry mapPoint = await MyMapView.SketchEditor.StartAsync(SketchCreationMode.Point, false);

            // Convert the map point to a screen point
            Point screenCoordinate = MyMapView.LocationToScreen((MapPoint)mapPoint);

            // Identify graphics in the graphics overlay using the point
            IReadOnlyList<IdentifyGraphicsOverlayResult> results = await MyMapView.IdentifyGraphicsOverlaysAsync(screenCoordinate, 2, false);

            // If results were found, get the first graphic
            Graphic graphic = null;
            IdentifyGraphicsOverlayResult idResult = results.FirstOrDefault();
            if (idResult != null && idResult.Graphics.Count > 0)
            { 
                graphic = idResult.Graphics.FirstOrDefault();
            }

            // Return the graphic (or null if none were found)
            return graphic;
        }
        #endregion

        private async void DrawButtonClick(object sender, RoutedEventArgs e)
        {
            try
            {
                // Let the user draw on the map view using the chosen sketch mode
                SketchCreationMode creationMode = (SketchCreationMode)SketchModeComboBox.SelectedItem;
                Esri.ArcGISRuntime.Geometry.Geometry geometry = await MyMapView.SketchEditor.StartAsync(creationMode, true);

                // Create and add a graphic from the geometry the user drew
                Graphic graphic = CreateGraphic(geometry);
                _sketchOverlay.Graphics.Add(graphic);

                // Enable/disable the clear and edit buttons according to whether or not graphics exist in the overlay
                ClearButton.IsEnabled = _sketchOverlay.Graphics.Count > 0;
                EditButton.IsEnabled = _sketchOverlay.Graphics.Count > 0;
            }
            catch (TaskCanceledException)
            {
                // Ignore ... let the user cancel drawing
            }
            catch (Exception ex)
            {
                // Report exceptions
                MessageBox.Show("Error drawing graphic shape: " + ex.Message);
            }
        }

        private void ClearButtonClick(object sender, RoutedEventArgs e)
        {
            // Remove all graphics from the graphics overlay
            _sketchOverlay.Graphics.Clear();

            // Disable buttons that require graphics
            ClearButton.IsEnabled = false;
            EditButton.IsEnabled = false;
        }

        private async void EditButtonClick(object sender, RoutedEventArgs e)
        {
            try
            {
                // Allow the user to select a graphic
                Graphic editGraphic = await GetGraphicAsync();
                if (editGraphic == null) { return; }

                // Let the user make changes to the graphic's geometry, await the result (updated geometry)
                Esri.ArcGISRuntime.Geometry.Geometry newGeometry = await MyMapView.SketchEditor.StartAsync(editGraphic.Geometry);

                // Display the updated geometry in the graphic
                editGraphic.Geometry = newGeometry;
            }
            catch (TaskCanceledException)
            {
                // Ignore ... let the user cancel editing
            }
            catch (Exception ex)
            {
                // Report exceptions
                MessageBox.Show("Error editing shape: " + ex.Message);
            }
        }
    }
}
XAML
<UserControl x:Class="ArcGISRuntime.WPF.Samples.SketchOnMap.SketchOnMap"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013" 
             mc:Ignorable="d" 
             d:DesignHeight="500" d:DesignWidth="600">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="300"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <esri:MapView x:Name="MyMapView" 
                      Grid.Column="1"/>
        <Grid Grid.Column="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition Height="35"/>
                <RowDefinition Height="35"/>
                <RowDefinition Height="35"/>
                <RowDefinition Height="20"/>
                <RowDefinition Height="35"/>
                <RowDefinition Height="35"/>
                <RowDefinition Height="20"/>
                <RowDefinition Height="35"/>
                <RowDefinition Height="35"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Row="1" Grid.Column="0" 
                       HorizontalAlignment="Right" VerticalAlignment="Center"
                       Text="Sketch shape:"/>
            <ComboBox x:Name="SketchModeComboBox"
                      Grid.Row="1" Grid.Column="1"
                      HorizontalAlignment="Stretch" VerticalAlignment="Center"
                      Margin="5"/>
            <Button Grid.Row="2" Grid.Column="1"
                    Content="Draw"
                    Margin="5"
                    Width="100" Height="25"
                    HorizontalAlignment="Center" VerticalAlignment="Top"
                    Click="DrawButtonClick"/>
            <Button x:Name="EditButton" 
                    Grid.Row="3" Grid.Column="1"
                    Content="Edit"
                    Margin="5"
                    Width="100" Height="25"
                    HorizontalAlignment="Center" VerticalAlignment="Top"
                    IsEnabled="False"
                    Click="EditButtonClick"/>
            <StackPanel Orientation="Horizontal"
                        HorizontalAlignment="Center" VerticalAlignment="Top"
                        Grid.Row="5" Grid.Column="1">
                <Button Content="Undo"
                        Margin="5"
                        Width="50" Height="25"
                        Command="{Binding UndoCommand}"/>
                <Button Content="Redo"
                        Margin="5"
                        Width="50" Height="25"
                        Command="{Binding RedoCommand}"/>
            </StackPanel>
            <Button Grid.Row="6" Grid.Column="1"
                    Content="Complete"
                    Margin="5"
                    Width="100" Height="25"
                    HorizontalAlignment="Center" VerticalAlignment="Top"
                    Command="{Binding CompleteCommand}"/>
            <Button Grid.Row="8" Grid.Column="1"
                    Content="Cancel"
                    Margin="5"
                    Width="100" Height="25"
                    HorizontalAlignment="Center" VerticalAlignment="Top"
                    Command="{Binding CancelCommand}"/>
            <Button x:Name="ClearButton" 
                    Grid.Row="9" Grid.Column="1"
                    Content="Clear"
                    Margin="5"
                    Width="100" Height="25"
                    IsEnabled="False"
                    HorizontalAlignment="Center" VerticalAlignment="Top"
                    Click="ClearButtonClick"/>
        </Grid>
    </Grid>
</UserControl>
See Also
Additional Examples
Hyperlink to ExampleDescription
CreateAndSaveKmlFileConstruct a KML document and save it as a KMZ file.
FindServiceAreaFind the service area within a network from a given point.
GeodatabaseTransactionsUse transactions to manage how changes are committed to a geodatabase.
SketchOnMapUse the Sketch Editor to edit or sketch a new point, line, or polygon geometry on to a map.