# What formulae does the Geolocation Distance use?

Forgive me if this is a bit extremely academic in nature, but I was wondering how Salesforce goes about determining the distance between two pairs of latitude and longitude?

When I've looked at this in the past it turned out not to be as straight forward as you might think. See Geographical distance and the second (inverse) geodetic problem. The earth is generally an ellipsoidal shape and there are discontinuities in the coordinate system. It's probably safe to assume changes in elevation aren't being taken into account in distance calculations.

Are they using a great-circle distance formula?

Why might this be important

How far is it from Wellington, New Zealand (41°19'S 174°49'E) to Salamanca, Spain(40°58'N 5°30'W)?

Depending on the model you use could be the difference between 19951 km, and 19960 km. Is that 0.04% difference important or even meaningful? Probably not for the majority of applications. Hence the statement at the start of the question that this is purely out of academic interest.

Using a Contact with a Location field set at -41.31666667 174.81666667 (Wellington) and the following SOQL query returned the Contact ID:

select Id from Contact WHERE DISTANCE(Location__c, GEOLOCATION(40.96666667, -5.50000000), 'km') > 19968.0

Increasing the distance to 19968.1 km did not.

Again, an 8 km difference wouldn't usually be considered significant on a ~20,000 km length where elevations are being ignored anyway.

• I actually can't find an answer for this, except "Distance is calculated as a straight line—as the crow flies—regardless of geography and topography between the two points." – sfdcfox Apr 12 '14 at 12:42
• Thanks for putting the bounty on this Daniel (although it doesn't seem to have awarded for some reason). That aside, I'm curious, did this ever go beyond academic interest in the two years between the time you asked this and when I answered it? – Alex Tennant May 20 '14 at 12:48
• @AlexTennant Odd, the bounty appears to have expired even though I accepted your answer. I would have thought it would have been awarded to you. I could offer a new bounty, but would need to double the rep to 300. I didn't get the 150 bounty points back from the last bounty I offered. – Daniel Ballinger May 20 '14 at 19:32
• I asked this question in April this year (2014). I haven't needed to make the distinction yet, but it is interesting. – Daniel Ballinger May 20 '14 at 19:35
• Shouldn't this post also be tagged with soql? – Adrian Larson Aug 29 '16 at 22:46

The documentation states:

Distance is calculated as a straight line—as the crow flies—regardless of geography and topography between the two points.

In reality Salesforce uses the haversine formula to calculate the great-circle distance between two points.

For any two points on a sphere, the haversine of the central angle between them is given by where

• haversin is the haversine function: • d is the distance between the two points (along a great circle of the sphere; see spherical distance),
• r is the radius of the sphere,
• : latitude of point 1 and latitude of point 2
• : longitude of point 1 and longitude of point 2

On the left side of the equals sign d/r is the central angle, assuming angles are measured in radians (note that φ and λ can be converted from degrees to radians by multiplying by π/180 as usual).

Solve for d by applying the inverse haversine (if available) or by using the arcsine (inverse sine) function: where h is haversin(d/r), or more explicitly:  You can confirm this by creating a custom object with two geolocation fields and a formula field which uses the DISTANCE formula to calculate the distance between the two locations, and then comparing the results to this haversine calculator.

Here are some examples:

Wellington (-41.31666667, 174.81666667) to Salamanca (40.96666667, -5.5)

• Salesforce: 19,968.02154120 km
• Haversine Calculator: 19970 km (to 4 SF)

London (51.507222, -0.1275) to Tokyo (35.689506, 139.6917)

• Salesforce: 9,558.73252169 km
• Haversine Calculator: 9559 km (to 4 SF)

Moscow (55.75, 37.616667) to Nice (43.7034, 7.2663)

• Salesforce: 2,529.41039443 km
• Haversine Calculator: 2529 km (to 4 SF)

Madrid (40.383333, -3.716667) to Sydney (-33.859972, 151.211111)

• Salesforce: 17,686.13743193 km
• Haversine Calculator: 17690 km (to 4 SF)

Pensacola (30.433333, -87.2) to Asmara (15.333333, 38.933333)

• Salesforce: 12,329.08258719 km
• Haversine Calculator: 12330 km (to 4 SF)

A couple of related posts have since appeared in the Salesforce documentation.

How SOQL Calculates and Compares Distances

The DISTANCE function approximates the haversine, or “great circle,” distance calculation within 0.0002%. This formula assumes that the Earth is a perfect sphere, when in fact it’s an ellipsoid: an irregular one. Errors from this assumption can be up to 0.55% crossing the equator, but are usually below 0.3%, depending on latitude and direction of travel.

The DISTANCE function is fine for calculating the 10 stores closest to a customer’s current location. But don’t fuel your plane for a flight from San Francisco to Sydney based on it.

The distance calculation is based on the haversine formula, which is used in mapping and navigation applications and is illustrated below. • I've studied a lot of math and trig over the years, yet have never heard of the Haversine formula! One never knows what they're going to learn when working with Salesforce! :) – crmprogdev May 6 '14 at 14:46