Purpose
Returns the distance between two locations. You can call this function from the MapBasic window in MapInfo Pro.
Syntax
CartesianDistance( x1, y1, x2, y2, unit_name )
x1 and x2 are x-coordinates.
y1 and y2 are y-coordinates.
unit_name is a string representing the name of a distance unit (e.g., "km").
Return Value
Float
Description
The CartesianDistance() function calculates the Cartesian distance between two locations. It 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 CartesianDistance() function always returns a value using a cartesian algorithm. A value of -1 is returned for data that is in a Latitude/Longitude coordinate system, since Latitude/Longitude data is not projected and not cartesian.
The x- and y-coordinate parameters must use MapBasic's current coordinate system. By default, MapInfo Pro expects coordinates to use a Latitude/Longitude coordinate system. You can reset MapBasic's coordinate system through the Set CoordSys statement.
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 = CartesianDistance(start_x,start_y,end_x,end_y,"mi")
See Also:
Math Functions, CartesianDistance() function, Distance() function