Click or drag to resize

SketchEditorStartAsync Method (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.

Namespace:  Esri.ArcGISRuntime.UI
Assembly:  Esri.ArcGISRuntime (in Esri.ArcGISRuntime.dll) Version: 100.11.0
Syntax
public Task<Geometry> StartAsync(
	SketchCreationMode creationMode,
	bool drawAndEdit = true
)

Parameters

creationMode
Type: Esri.ArcGISRuntime.UISketchCreationMode
Determines the type of geometry and how it will be created.
drawAndEdit (Optional)
Type: SystemBoolean
Optional parameter that indicates whether to proceed to edit. If false, sketch will complete once drawn.

Return Value

Type: TaskGeometry
A task that represents the asynchronous sketching of geometry. The value of the task result is a Geometry object.
Remarks

Each time one of the StartAsync methods is called on the SketchEditor, it creates a default SketchEditConfiguration with parameters that make sense for that particular shape (e.g. circles are scaled in an aspect-ratio-preserving way, but ellipses are not). The intended use of the EditConfiguration property is to override these defaults after each StartAsync call, to modify the defaults based on context. You can use any one of the StartAsync overloaded methods which accepts a SketchEditConfiguration parameter, or modify the EditConfiguration after calling StartAsync.

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.