Share via


Distance method

Returns the distance between two locations, in GeoUnits (miles or kilometers) as Double.

Applies to

Objects:  Map

Syntax

object.Distance(StartLocation, EndLocation)

Parameters

Part Description
object Required. An expression that returns a Map object.
StartLocation Required Location object. The start point of the distance to be measured.
EndLocation Required Location object. The end point of the distance to be measured.

Remarks

To return the distance to a location, route, or route segment from another location, use the DistanceTo method on a Directions collection or Direction or Location object.

To return or set GeoUnits, use the Units property of an Application or MappointControl object.

Example

  [Microsoft Visual Basic 6.0]
Sub ShowDistanceNYToLA()   Dim objApp As New MapPoint.Application   Dim objMap As MapPoint.Map   Dim objLocNY As MapPoint.Location   Dim objLocLA As MapPoint.Location   Dim objCenter As MapPoint.Location   Dim objTextbox As MapPoint.Shape
  'Set up application and create Location objects   Let objApp.Visible = True   Let objApp.UserControl = True   Set objMap = objApp.ActiveMap   Set objLocNY = objMap.FindResults("New York, NY").Item(1)   Set objLocLA = objMap.FindResults("Los Angeles, CA").Item(1)
  'Find the point on the screen midway between the two   X1% = objMap.LocationToX(objLocNY)   Y1% = objMap.LocationToY(objLocNY)   X2% = objMap.LocationToX(objLocLA)   Y2% = objMap.LocationToY(objLocLA)   x% = (X1% + X2%) / 2   y% = (Y1% + Y2%) / 2   Set objCenter = objMap.XYToLocation(x%, y%)
  'Show the distance   objMap.Shapes.AddLine objLocNY, objLocLA   Set objTextbox = objMap.Shapes.AddTextbox(objCenter, 200, 50)   Distance# = objMap.Distance(objLocNY, objLocLA)   Let objTextbox.Text = "Distance, New York to Los Angeles: " & Distance# End Sub
[C#]
void ShowDistanceNYtoLA() { ApplicationClass objApp = null; MapPoint.Map objMap; MapPoint.Shape objTextBox; String Place1 = "New York, NY"; String Place2 = "Los Angeles,CA"; double Distance; //Create an application class instance objApp = new ApplicationClass(); //Set up application objApp.Visible = true; objApp.UserControl = true; objMap = objApp.ActiveMap; object Item = 1; MapPoint.FindResults obNY = objMap.FindResults(Place1); MapPoint.FindResults obLA= objMap.FindResults(Place2); MapPoint.Location objNY = obNY.get_Item(ref Item) as MapPoint.Location; MapPoint.Location objLA = obLA.get_Item(ref Item) as MapPoint.Location; objNY.GoTo(); objLA.GoTo(); MapPoint.SelectedArea sa = objMap.SelectedArea; MapPoint.Location objCenter = objMap.XYToLocation(sa.Left + sa.Width, sa.Top + sa.Height); objMap.Shapes.AddShape(MapPoint.GeoAutoShapeType.geoShapeOval, objLA, 50, 30); objMap.Shapes.AddLine(objNY.Location, objLA.Location); Distance = objMap.Distance(objNY, objLA); objTextBox = objMap.Shapes.AddTextbox(objCenter, 100, 50); objTextBox.Text = ("Distance, NY to LA: " + Distance); objMap.Altitude = 350; }

Note  This sample code is specifically for use in MapPoint North America; it is for illustration purposes only.