Click or drag to resize
Code Example - ProximityResult

Demonstrates identifying the nearest vertex and nearest coordinate in a polyline to a user clicked point in the map via ProximityResult.

Code Example
Proximity Result

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="ProximityResult.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">

        <!-- Grid Resources section that define the various symbols used in the map. -->
        <Grid.Resources>

            <!-- The marker drawn on the map from the user clicks.-->
            <esri:SimpleMarkerSymbol x:Key="PointSymbol_UserClick" Color="Black" Size="20" />

            <!-- A Polyline on the map where the user can click around to see the results of the ProximityResult. -->
            <esri:SimpleLineSymbol x:Key="LineSymbol" Color="Blue" Style="Solid" Width="4"/>

            <!-- ProximityResult symbol from using the GeometryEngine.NeatestVertext Method.-->
            <esri:SimpleMarkerSymbol x:Key="PointSymbol_ProximityResult_NearestVertex" Color="Yellow" Size="20" />

            <!-- ProximityResult symbol for using the GeometryEngine.NEarestCoordinate Method.-->
            <esri:SimpleMarkerSymbol x:Key="PointSymbol_ProximityResult_NearestCoordinate" Color="Red" Size="20" />

        </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 polyline on the map. -->
                <esri:GraphicsLayer x:Name="myGraphicsLayer_Polyline"/>

                <!-- GraphicsLayer to hold the user clicked  MapPoint on the map. -->
                <esri:GraphicsLayer x:Name="myGraphicsLayer_UserClick_MapPoint"/>

                <!-- GraphicsLayer to hold the result of the NearestVertex on the polyline from where the user clicked. -->
                <esri:GraphicsLayer x:Name="myGraphicsLayer_ProximityResult_NearestVertex"/>

                <!-- GraphicsLayer to hold the result of the NearestCoordinate in the polyline from where the user clicked. -->
                <esri:GraphicsLayer x:Name="myGraphicsLayer_ProximityResult_NearestCoordinate"/>

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

        <StackPanel Orientation="Horizontal">

            <!-- Display ProximityResult information to from the GeoemtryEngine.NearestVertex operation. -->
            <Border Background="Yellow" BorderBrush="Black" BorderThickness="2" Margin="5" HorizontalAlignment="Right" VerticalAlignment="Bottom">
                <StackPanel Margin="3,3" Orientation="Vertical">
                    <TextBlock Text="Proximity Results (Nearest Vertex)" TextAlignment="Left" TextWrapping="Wrap" FontWeight="Bold" />
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Distance:"/>
                        <TextBox x:Name="TextBox_Distance" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="PartIndex:"/>
                        <TextBox x:Name="TextBox_PartIndex" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Point:"/>
                        <TextBox x:Name="TextBox_Point" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="PointIndex:"/>
                        <TextBox x:Name="TextBox_PointIndex" />
                    </StackPanel>
                </StackPanel>
            </Border>

            <!-- Display ProximityResult information to from the GeoemtryEngine.NearestCoordinate operation. -->
            <Border Background="Red" BorderBrush="Black" BorderThickness="2" Margin="5" HorizontalAlignment="Right" VerticalAlignment="Bottom">
                <StackPanel Margin="3,3" Orientation="Vertical">
                    <TextBlock Text="Proximity Results (Nearest Coordinate)" TextAlignment="Left" TextWrapping="Wrap" FontWeight="Bold"/>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Distance:"/>
                        <TextBox x:Name="TextBox_Distance_C" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="PartIndex:"/>
                        <TextBox x:Name="TextBox_PartIndex_C" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Point:"/>
                        <TextBox x:Name="TextBox_Point_C" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="PointIndex:"/>
                        <TextBox x:Name="TextBox_PointIndex_C" />
                    </StackPanel>
                </StackPanel>
            </Border>
        </StackPanel>
    </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 "ProximityResult.MainWindow" to be just "MainWindow".

// INSTRUCTIONS:
// Click in various places around the polyline on the map and a black dot will appear where you click.
// The closest vertex in the polyline to where you clicked will display as a yellow dot. Additionally,
// the closest point in the line segment that comprises the polyline will be displayed as a red dot.
// In some instances the yellow dot and the red dot will be in exactly the same place depending on where 
// you clicked in the map. Click several places in the map to see the the various closest vertex (via
// GeometryEngine.NearestVertex) and closest point (via GeometryEngine.NearestCoordinate) to the 
// polyline from where you clicked on the map.

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

            // Set the initial extent (via the InitialViewpoint Property) to that of the central US. 
            Esri.ArcGISRuntime.Geometry.Envelope myEnvelope = new Esri.ArcGISRuntime.Geometry.Envelope(-13342245.78, 3112299.55, -9149732.72, 6112299.33);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myEnvelope);

            // Create a set of MapPoint coordinates to create a polyline on the map.
            System.Collections.Generic.List<Esri.ArcGISRuntime.Geometry.MapPoint> myPart = new System.Collections.Generic.List<Esri.ArcGISRuntime.Geometry.MapPoint>();
            myPart.Add(new Esri.ArcGISRuntime.Geometry.MapPoint(-12860962.5668449, 4909090.90909091));
            myPart.Add(new Esri.ArcGISRuntime.Geometry.MapPoint(-12090909.0909091, 4556149.73262032));
            myPart.Add(new Esri.ArcGISRuntime.Geometry.MapPoint(-11395721.9251337, 4802139.03743315));
            myPart.Add(new Esri.ArcGISRuntime.Geometry.MapPoint(-10465240.6417112, 4866310.16042781));
            myPart.Add(new Esri.ArcGISRuntime.Geometry.MapPoint(-9513368.98395722, 4695187.1657754));

            // Create a polyline from MapPoint coordinates. 
            Esri.ArcGISRuntime.Geometry.Polyline myPolyline = new Esri.ArcGISRuntime.Geometry.Polyline(myPart);

            // Get polyline symbol defined in XAML and apply it to the Polyline graphics layer.
            Esri.ArcGISRuntime.Symbology.Symbol myPolylineSymbol = (Esri.ArcGISRuntime.Symbology.Symbol)(layoutGrid.Resources["LineSymbol"]);
            myGraphicsLayer_Polyline.Graphics.Add(new Esri.ArcGISRuntime.Layers.Graphic(myPolyline, myPolylineSymbol));
        }

        private void mapView_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // For each mouse down click, clear out the graphics in the various GraphicsLayer's. 
            myGraphicsLayer_UserClick_MapPoint.Graphics.Clear();
            myGraphicsLayer_ProximityResult_NearestVertex.Graphics.Clear();
            myGraphicsLayer_ProximityResult_NearestCoordinate.Graphics.Clear();

            try
            {
                // Convert the screen point to the map point.
                System.Windows.Point myUIPointLocation = e.GetPosition(MapView1);
                Esri.ArcGISRuntime.Geometry.MapPoint myMapPoint = MapView1.ScreenToLocation(myUIPointLocation);

                // Show geometries on map.
                if (myGraphicsLayer_UserClick_MapPoint != 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_UserClick"]);
                    myGraphicsLayer_UserClick_MapPoint.Graphics.Add(new Esri.ArcGISRuntime.Layers.Graphic(myMapPoint, myPointSymbol));

                    // Get the geometry of the polyline that is drawn on the map.
                    Esri.ArcGISRuntime.Geometry.Polyline myPolyline = (Esri.ArcGISRuntime.Geometry.Polyline)myGraphicsLayer_Polyline.Graphics[0].Geometry;

                    // Use the GeometryEngine.NearestVertex Method to find the nearest vertex in the polyline from where the user clicked on the map. 
                    Esri.ArcGISRuntime.Geometry.ProximityResult myProximityResult_NearestVertex = Esri.ArcGISRuntime.Geometry.GeometryEngine.NearestVertex(myPolyline, myMapPoint);

                    // Display the various ProximityResult Properties in the TextBoxes. 
                    TextBox_Distance.Text = myProximityResult_NearestVertex.Distance.ToString();
                    TextBox_PartIndex.Text = myProximityResult_NearestVertex.PartIndex.ToString();
                    TextBox_Point.Text = myProximityResult_NearestVertex.Point.X.ToString() + ", " + myProximityResult_NearestVertex.Point.Y.ToString();
                    TextBox_PointIndex.Text = myProximityResult_NearestVertex.PointIndex.ToString();

                    // Add the ProximityResult MapPoint from the GeometryEngine.NearestVertex operation on the screen.
                    Esri.ArcGISRuntime.Symbology.Symbol myProximityResultPointSymbol_NearestVertex = (Esri.ArcGISRuntime.Symbology.Symbol)(layoutGrid.Resources["PointSymbol_ProximityResult_NearestVertex"]);
                    myGraphicsLayer_ProximityResult_NearestVertex.Graphics.Add(new Esri.ArcGISRuntime.Layers.Graphic(myProximityResult_NearestVertex.Point, myProximityResultPointSymbol_NearestVertex));

                    //-------------------

                    // Use the GeometryEngine.NearestCoordinate Method to find the nearest point location along the polyline from where the user clicked on the map.
                    Esri.ArcGISRuntime.Geometry.ProximityResult myProximityResult_NearestCoordinate = Esri.ArcGISRuntime.Geometry.GeometryEngine.NearestCoordinate(myPolyline, myMapPoint);

                    // Display the various ProximityResult Properties in the TextBoxes. 
                    TextBox_Distance_C.Text = myProximityResult_NearestCoordinate.Distance.ToString();
                    TextBox_PartIndex_C.Text = myProximityResult_NearestCoordinate.PartIndex.ToString();
                    TextBox_Point_C.Text = myProximityResult_NearestCoordinate.Point.X.ToString() + ", " + myProximityResult_NearestCoordinate.Point.Y.ToString();
                    TextBox_PointIndex_C.Text = myProximityResult_NearestCoordinate.PointIndex.ToString();

                    // Add the ProximityResult MapPoint from the GeometryEngine.NearestCoordinate operation on the screen.
                    Esri.ArcGISRuntime.Symbology.Symbol myProximityResultPointSymbol_C = (Esri.ArcGISRuntime.Symbology.Symbol)(layoutGrid.Resources["PointSymbol_ProximityResult_NearestCoordinate"]);
                    myGraphicsLayer_ProximityResult_NearestCoordinate.Graphics.Add(new Esri.ArcGISRuntime.Layers.Graphic(myProximityResult_NearestCoordinate.Point, myProximityResultPointSymbol_C));
                }
            }
            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!");
            }
        }
    }
}