How To: Find the distance between two features using IProximityOperator::ReturnDistance


The sample code in this article returns the distance between two features using VBA and ArcObjects.



  1. Open ArcMap.
  2. Create a new UIButtonControl: How To: Create a new UIButtonControl​
    For more information on creating a UIControl, see the ArcGIS Desktop Help topic: 'Creating custom commands with VBA and UI Controls'
  3. Right-click the UIButtonControl and select View Source.
  4. Paste this code into the UIButtonControl's click event.
    Dim pDoc As IMxDocument
    Dim pMap As IMap
    Set pDoc = ThisDocument
    Set pMap = pDoc.FocusMap
    Dim pFLayer As IFeatureLayer
    Set pFLayer = pMap.Layer(0)
    Dim pFeat As IFeature
    Dim pFeat1 As IFeature
    Dim dDist As Double
    Dim pFc As IFeatureClass
    Set pFc = pFLayer.FeatureClass
    'Get features 1 and 4
    Set pFeat = pFc.GetFeature(1)
    Set pFeat1 = pFc.GetFeature(4)
    Dim pProxOp As IProximityOperator
    Dim pGeom As IGeometry
    Dim pGeom1 As IGeometry
    Set pGeom1 = pFeat1.Shape
    Set pGeom = pFeat.Shape
    Set pProxOp = pGeom
    'Find the Distance
    dDist = pProxOp.ReturnDistance(pGeom1)
    MsgBox "The distance is: " & dDist
  5. Close the Visual Basic Editor.
  6. Add data to ArcMap.
  7. Click the new button to display the distance.