This document is archived and information here might be outdated.  Recommended version.


Get Distance from Two Points Snippet (ArcObjects .NET 10.4 SDK)
ArcObjects Library Reference

Get Distance from Two Points Snippet

Get the geodetically correct Rhumb Line distance between two points.

[C#]
///<summary>Get the geodetically correct Rhumb Line distance between two points.</summary>
/// 
///<param name="fromPoint">An IPoint interface that is the start (or from) location</param>
///<param name="toPoint">An IPoint interface that is the end (or to) location</param>
///<param name="spatialReference">An esriSRGeoCSType enum that is a predefined geographic coordinate system. Example: ESRI.ArcGIS.Geometry.esriSRGeoCSType.esriSRGeoCS_NAD1983</param>
///  
///<returns>A System.Double representing true distance</returns>
///  
///<remarks></remarks>
public System.Double GetDistanceFromTwoPoints(ESRI.ArcGIS.Geometry.IPoint fromPoint, ESRI.ArcGIS.Geometry.IPoint toPoint, ESRI.ArcGIS.Geometry.esriSRGeoCSType spatialReference)
{

  // Define the spatial reference of the rhumb line. 
  ESRI.ArcGIS.Geometry.ISpatialReferenceFactory2 spatialReferenceFactory2=new ESRI.ArcGIS.Geometry.SpatialReferenceEnvironmentClass();
  ESRI.ArcGIS.Geometry.ISpatialReference2 spatialReference2=(ESRI.ArcGIS.Geometry.ISpatialReference2)spatialReferenceFactory2.CreateSpatialReference((System.Int16)spatialReference);

  // Initialize the MeasurementTool and define the properties of the line.
  // These properties include the line type, which is a rhumb line in this case, and the 
  // spatial reference of the line.   
  ESRI.ArcGIS.DefenseSolutions.IMeasurementTool measurementTool=new ESRI.ArcGIS.DefenseSolutions.MeasurementToolClass();
  measurementTool.SpecialGeolineType=ESRI.ArcGIS.DefenseSolutions.cjmtkSGType.cjmtkSGTRhumbLine;
  measurementTool.SpecialSpatialReference=spatialReference2;

  // Determine the distance and azimuth of the rhumb line based on the start and end point coordinates.   
  measurementTool.ConstructByPoints(fromPoint, toPoint);

  // Return the Distance. 
  return measurementTool.Distance;
}
[Visual Basic .NET]
'''<summary>Get the geodetically correct Rhumb Line distance between two points.</summary>
''' 
'''<param name="fromPoint">An IPoint interface that is the start (or from) location</param>
'''<param name="toPoint">An IPoint interface that is the end (or to) location</param>
'''<param name="spatialReference">An esriSRGeoCSType enum that is a predefined geographic coordinate system. Example: ESRI.ArcGIS.Geometry.esriSRGeoCSType.esriSRGeoCS_NAD1983</param>
'''  
'''<returns>A System.Double representing true distance</returns>
'''  
'''<remarks></remarks>
Public Function GetDistanceFromTwoPoints(ByVal fromPoint As ESRI.ArcGIS.Geometry.IPoint, ByVal toPoint As ESRI.ArcGIS.Geometry.IPoint, ByVal spatialReference As ESRI.ArcGIS.Geometry.esriSRGeoCSType) As System.Double

  'Define the spatial reference of the rhumb line. 
  Dim spatialReferenceFactory2 As ESRI.ArcGIS.Geometry.ISpatialReferenceFactory2=New ESRI.ArcGIS.Geometry.SpatialReferenceEnvironmentClass
  Dim spatialReference2 As ESRI.ArcGIS.Geometry.ISpatialReference2=CType(spatialReferenceFactory2.CreateSpatialReference(spatialReference), ESRI.ArcGIS.Geometry.ISpatialReference2)

  'Initialize the MeasurementTool and define the properties of the line.
  'These properties include the line type, which is a rhumb line in this case, and the 
  'spatial reference of the line. 
  Dim measurementTool As ESRI.ArcGIS.DefenseSolutions.IMeasurementTool=New ESRI.ArcGIS.DefenseSolutions.MeasurementToolClass
  measurementTool.SpecialGeolineType=ESRI.ArcGIS.DefenseSolutions.cjmtkSGType.cjmtkSGTRhumbLine
  measurementTool.SpecialSpatialReference=spatialReference2

  'Determine the distance and azimuth of the rhumb line based on the 
  'start and end point coordinates. 
  measurementTool.ConstructByPoints(fromPoint, toPoint)

  'Return the Distance
  Return measurementTool.Distance

End Function

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.DefenseSolutions
  • ESRI.ArcGIS.Geometry
  • ESRI.ArcGIS.System