Distance() function - MapBasic - 2023

MapInfo MapBasic Reference

Product type
Software
Portfolio
Locate
Product family
MapInfo
Product
MapInfo > MapBasic
Version
2023
Language
English
Product name
MapBasic
Title
MapInfo MapBasic Reference
First publish date
1985
Last updated
2023-09-12
Published on
2023-09-12T16:32:32.686312

Purpose

Returns the distance between two locations. You can call this function from the MapBasic window in MapInfo Pro.

Syntax

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

Float

Description

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 Pro expects coordinates to use a Longitude/Latitude coordinate system. You can reset MapBasic's coordinate system 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 Pro 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.

Example

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")

See Also:

Area() function, ObjectLen() function, Set CoordSys statement, Set Distance Units statement