MapInfo Professional: Using the Distance () Function

Products Affected: MapInfo Pro™, MapBasic™
Distance( ) function

Returns the distance between two locations. This function can be called from the MapBasic window in MapInfo Professional.

Distance ( x1, y1, x2, y2, unit_name )
x1 and x2 are x-coordinates (for example, longitude).

y1 and y2 are y-coordinates (for example, latitude).

unit_name is a string representing the name of a distance unit (for example, "km").

Return Value

The Distance( ) function calculates the distance between two locations.

The function returns the distance measurement in the units specified by the unit_name parameter; for example, to obtain a distance in miles, specify "mi" as the unit_name parameter. See Set Distance Units statement for the list of available unit names.

The x- and y-coordinate parameters must use MapBasic's current coordinate system. By default, MapInfo Professional expects coordinates to use a Longitude/Latitude coordinate system. MapBasic's coordinate system can be reset through the Set CoordSys statement.

If the current coordinate system is an earth coordinate system, Distance( ) returns the great-circle distance between the two points. A great-circle distance is the shortest distance between two points on a sphere. (A great circle is a circle that goes around the earth, with the circle's center at the center of the earth; a great-circle distance between two points is the distance along the great circle which connects the two points.)

For the most part, MapInfo Professional performs a Cartesian or Spherical operation. Generally, a spherical operation is performed unless the coordinate system is NonEarth, in which case, a Cartesian operation is performed.

Dim dist, start_x, start_y, end_x, end_y As Float
Open Table "cities"
Fetch First From cities
start_x = CentroidX(cities.obj)
start_y = CentroidY(cities.obj)
Fetch Next From cities
end_x = CentroidX(cities.obj)
end_y = CentroidY(cities.obj) 
dist = Distance(start_x,start_y,end_x,end_y,"mi")
UPDATED:  December 2, 2019