# Distance From Geometry

### Description

This sample demonstrates using the GeometryEngine.DistanceFromGeometry method to calculate the linear distance of the shortest length between two geometries. To use the sample, click on the 'Calculate Distance' button and then add a polyline and a point to the map. After the point is entered the shortest distance between them is displayed.

Available for Desktop, Store, Phone

## Sample Code

``````﻿<UserControl x:Class="ArcGISRuntime.Samples.Desktop.DistanceFromGeometry"
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">
<Grid x:Name="layoutGrid">
<Grid.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>

<esri:SimpleLineSymbol x:Key="LineSymbol" Color="Black" Width="2" Style="Solid" />
<esri:SimpleMarkerSymbol x:Key="PointSymbol" Color="Black" Style="Circle" Size="6" />
</Grid.Resources>

<esri:MapView x:Name="MyMapView" WrapAround="True">
<esri:Map InitialViewpoint="-15053000,2749000,-6540000,6590000">
<esri:ArcGISTiledMapServiceLayer
ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer" />
</esri:Map>
<esri:MapView.GraphicsOverlays>
<esri:GraphicsOverlay ID="graphicsOverlay" />
</esri:MapView.GraphicsOverlays>
</esri:MapView>

<Border Background="White" BorderBrush="Black" BorderThickness="1"
HorizontalAlignment="Right" VerticalAlignment="Top"
<Border.Effect>
</Border.Effect>
<StackPanel>
<TextBlock Text="Click on the 'Calculate Distance' button to add a polyline and then a point to the map.  After the point is entered the shortest distance between them is displayed."
Width="400" TextWrapping="Wrap" />
<Button Content="Calculate Distance" Margin="12,12,12,0" HorizontalAlignment="Center"
Click="DistanceButton_Click" />
<TextBlock x:Name="txtResults" Margin="8,16,8,0" HorizontalAlignment="Center" FontSize="14" Visibility="Collapsed" />
</StackPanel>
</Border>

<Border Background="White" BorderBrush="Black" BorderThickness="2" Margin="25"
HorizontalAlignment="Center" VerticalAlignment="Bottom"
Visibility="{Binding ElementName=MyMapView, Path=Editor.IsActive, Converter={StaticResource BooleanToVisibilityConverter}}">
<TextBlock Text="Draw a polyline and a point on the map." Margin="8" FontSize="14" />
</Border>
</Grid>
</UserControl>
``````
``````﻿using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Symbology;
using System;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
/// <summary>
/// This sample demonstrates using the GeometryEngine.DistanceFromGeometry method to calculate the linear distance of the shortest length between two geometries. To use the sample, click on the 'Calculate Distance' button and then add a polyline and a point to the map. After the point is entered the shortest distance between them is displayed.
/// </summary>
/// <title>Distance From Geometry</title>
/// <category>Geometry</category>
public partial class DistanceFromGeometry : UserControl
{
private const double METERS_TO_MILES = 0.0006213700922;

private Symbol _lineSymbol;
private Symbol _pointSymbol;
private GraphicsOverlay _graphicsOverlay;

/// <summary>Construct Distance From Geometry sample control</summary>
public DistanceFromGeometry()
{
InitializeComponent();

_lineSymbol = layoutGrid.Resources["LineSymbol"] as Symbol;
_pointSymbol = layoutGrid.Resources["PointSymbol"] as Symbol;
_graphicsOverlay = MyMapView.GraphicsOverlays["graphicsOverlay"];
}

// Calculates the linear distance between two user-defined geometries
private async void DistanceButton_Click(object sender, RoutedEventArgs e)
{
try
{
txtResults.Visibility = Visibility.Collapsed;
_graphicsOverlay.Graphics.Clear();

// wait for user to draw a polyline
var line = await MyMapView.Editor.RequestShapeAsync(DrawShape.Polyline, _lineSymbol);

// wait for user to draw a point
var point = await MyMapView.Editor.RequestPointAsync();

// Calculate distance between line and point
double distance = GeometryEngine.Distance(line, point) * METERS_TO_MILES;
txtResults.Text = string.Format("Distance between geometries: {0:0.000} miles", distance);
txtResults.Visibility = Visibility.Visible;
}
catch (Exception ex)
{
MessageBox.Show("Distance Calculation Error: " + ex.Message, "Distance From Geometry Sample");
txtResults.Visibility = Visibility.Collapsed;
_graphicsOverlay.Graphics.Clear();
}
}
}
}
``````
Feedback on this topic?