SphericalDistance() 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. You can call this function from the MapBasic window in MapInfo Pro.

Syntax

SphericalDistance( 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 SphericalDistance() 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 Latitude/Longitude coordinate system. You can reset MapBasic's coordinate system through the Set CoordSys statement.

The SphericalDistance() function always returns a value as calculated in a Latitude/Longitude non-projected coordinate system using great circle based algorithms. A value of -1 will be returned for data that is in a NonEarth coordinate system since this data cannot be converted into a Latitude/longitude coordinate system.

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 = SphericalDistance(start_x,start_y,end_x,end_y,"mi")

See Also:

CartesianDistance() function, Distance() function