# haversine formula

This topic is 2824 days old. Please don't post here. Open a new topic instead.

## Recommended Posts

for shortest distance between two points along great circle
someone may already have done this...
or you can write a custom function to do the same thing..

```Let ( [
R = 6372.8 ;
dLat = Radians ( laty - latx ) ;
dLon = Radians ( lony - lonx ) ;
lati = Radians ( latx ) ;
latj = Radians ( laty ) ;
a = Sin ( dLat / 2 ) * Sin ( dLat / 2 ) + Sin ( dLon / 2 ) * Sin ( dLon / 2 ) * Cos ( lati ) * Cos ( latj ) ;
c = 2 * Asin ( Sqrt ( a ) ) ;
M = R * c * .621371192
] ;
Case ( IsEmpty ( units ) ; Int ( M ) & " mi " & Round ( ( M - Int ( M ) ) * 1760 ; 2 ) & " yds" ; Round ( R * c ; 3 ) & " km" )
)
```
```// HaversineDistance ( latx ; lonx ; laty ; lony ; units )
// 15_02_03 JR
// v1.0
// returns shortest diatance by Haversine method
// if units then it is in KM else miles and yds

R = 6372.8d

lati = latx.toDouble()
loni = lonx.toDouble()
latj = laty.toDouble()
lonj = lony.toDouble()

a = Math.sin(dLat / 2 ) * Math.sin (dLat / 2 ) + Math.sin(dLon / 2) * Math.sin(dLon / 2 ) * Math.cos(lati) * Math.cos(latj)
c = 2 * Math.asin(Math.sqrt( a ))
if ( units ){
return (R * c).round(3) + ' km'
} else {
M = ((R * c) * 0.621371192)
return M.toInteger() + ' mi ' + ((M - M.toInteger()) * 1760).round(2) + ' yds'
} //end if
```
##### Share on other sites

What's the benefit of doing this with ScriptMaster instead of as a custom function?

Presuming that the goal is to calculate distances between two points on the surface of the Earth (based on the radius being used), there's also a custom function using a different formula based on an ellipsoidal rather than spherical approximation, and so is more accurate (and more complicated).

##### Share on other sites

Nice CF JB...

I had written a ScriptMaster function to use a SOAP call to a site returning geo-location information, and adding this to the parsing of the JSON meant I could return the information they are really after in one step.

##### Share on other sites

If you're sticking to ScriptMaster and accuracy is really important to you GeographicLib is the gold standard. It is, paradoxically, more accurate than the actual surface of the Earth.

##### Share on other sites

This topic is 2824 days old. Please don't post here. Open a new topic instead.

## Create an account

Register a new account