Click or drag to resize
Code Example - GeometryEngine_Buffer

Demonstrates using the powerful GeometryEngine local process to generate graphic buffer polygons based upon a user specified radius and mouse-clicks/taps on the map.

Code Example
Geometry Engine Buffer

This section contains selected code files from a Visual Studio project that emphasize specific ArcGIS Runtime SDK (Windows Desktop) features. For example: some code examples may accomplish the bulk of the work as a configuration property in the .xaml file and hence only the .xaml file will be shown in detail below. In other code examples, the .xaml is used to define the configuration of graphical elements for the application but the application logic is performed in the code behind, hence you may see both the .xaml and .cs/.vb files shown in detail below.

XAML
<Window x:Class="GeometryEngine_Buffer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
        Title="MainWindow" Height="600" Width="800">

    <Grid x:Name="layoutGrid">

        <!-- Define a SimpleMarkerSymbol and SimpleFillSymbol in the Grid Resources. -->
        <Grid.Resources>

            <!-- A red point, size 10.-->
            <esri:SimpleMarkerSymbol x:Key="PointSymbol" Color="Red" Size="10" />

            <!-- A semi transparent blue fill, with blue border, thickness of 2. -->
            <esri:SimpleFillSymbol x:Key="BufferSymbol" Color="#660000FF" Style="Solid">
                <esri:SimpleFillSymbol.Outline>
                    <esri:SimpleLineSymbol Color="Blue" Width="2" />
                </esri:SimpleFillSymbol.Outline>
            </esri:SimpleFillSymbol>

        </Grid.Resources>

        <!-- Add a MapView. -->
        <esri:MapView x:Name="MapView1" WrapAround="True" MouseDown="mapView_MouseDown">

            <!-- Add a Map.-->
            <esri:Map x:Name="Map1">

                <!-- Add a backdrop ArcGISTiledMapServiceLayer. -->
                <esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" />

                <!-- GraphicsLayer to hold the points the user clicks on the map. -->
                <esri:GraphicsLayer x:Name="myGraphicsLayer_UserInputPoints"/>

                <!-- GraphicsLayer to hold the polygons from the GeometryEngine.Buffer operation. -->
                <esri:GraphicsLayer x:Name="myGraphicsLayer_BufferPolygon"/>

            </esri:Map>
        </esri:MapView>

        <!-- Add instructions on how to use the example code. -->
        <Border Background="White" BorderBrush="Black" BorderThickness="2" Margin="5" HorizontalAlignment="Right" VerticalAlignment="Bottom">
            <StackPanel Margin="3,3" Orientation="Horizontal">
                <TextBlock Text="Specify the buffer in miles and then click on the map:"
                           TextAlignment="Left" TextWrapping="Wrap" />
                <TextBox x:Name="TextBoxMiles" Text="5" Width="25" TextAlignment="right"/>
            </StackPanel>
        </Border>
    </Grid>

</Window>

SPECIAL NOTE: The XAML displayed above comes from a C# project. If you are a VB.NET developer, you will need to modify the text for the x:Class namespace from "GeometryEngine_Buffer.MainWindow" to be just "MainWindow".

namespace GeometryEngine_Buffer
{

    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Set the initial extent (via the InitialViewpoint Property) of the backdrop ArcGIStiledMapServiceLayer to the Dallas/Fort Worth area.
            Esri.ArcGISRuntime.Geometry.Geometry myGeometry = new Esri.ArcGISRuntime.Geometry.Envelope(-10863035.97, 3838021.34, -10744801.344, 3887145.299);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myGeometry);
        }

        private void mapView_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                // Convert the screen point to the map point.
                System.Windows.Point myUIPointLocation = e.GetPosition(MapView1);
                Esri.ArcGISRuntime.Geometry.MapPoint myMapPoint = MapView1.ScreenToLocation(myUIPointLocation);

                // Get the buffer size from the user.
                double myBufferInMiles = System.Convert.ToDouble(TextBoxMiles.Text);

                // Create a variable to be the buffer size in meters. There are 1609.34 meters in one mile.
                double myBufferInMeters = myBufferInMiles * 1609.34;

                // Get a buffer polygon from the GeometryEngine Buffer operation. 
                // Note: the input distance to the Buffer operation is in meters. This is because the backdrop ArcGISTiledMapServiceLayer set the MapView's 
                // SpatialReference to WKID=102100, units=esriMeters.
                Esri.ArcGISRuntime.Geometry.Geometry myGeometryBuffer = Esri.ArcGISRuntime.Geometry.GeometryEngine.Buffer(myMapPoint, myBufferInMeters);

                // Show geometries on map.
                if (myGraphicsLayer_UserInputPoints != null)
                {
                    // Get point symbol defined in XAML and apply it to the user clicked location; then add the map point to the graphics layer.
                    Esri.ArcGISRuntime.Symbology.Symbol myPointSymbol = (Esri.ArcGISRuntime.Symbology.Symbol)(layoutGrid.Resources["PointSymbol"]);
                    myGraphicsLayer_UserInputPoints.Graphics.Add(new Esri.ArcGISRuntime.Layers.Graphic(myMapPoint, myPointSymbol));

                    // Get polygon symbol defined in XAML and apply it to the buffer graphic; then add the buffer polygon to the graphics layer.
                    Esri.ArcGISRuntime.Symbology.Symbol myPolygonSymbol = (Esri.ArcGISRuntime.Symbology.Symbol)(layoutGrid.Resources["BufferSymbol"]);
                    myGraphicsLayer_BufferPolygon.Graphics.Add(new Esri.ArcGISRuntime.Layers.Graphic(myGeometryBuffer, myPolygonSymbol));
                }
            }
            catch (System.Exception ex)
            {
                // Display an error message if there is a problem generating the buffer polygon.
                System.Windows.MessageBox.Show(ex.Message, "Geometry Engine Failed!");
            }

        }


    }
}