3 Replies Latest reply on Jun 5, 2016 3:27 AM by db49er

# Custom function for range and bearing between two geographical points

Does anyone know of a custom function to measure the bearing and distance between two latitudes and longitudes?

Thank you

• ###### 2. Re: Custom function for range and bearing between two geographical points

DISTANCE

Let ( [

R = 6373 ;

lon1 = longitude1 / 180 * Pi ;

lon2 = longitude2 / 180 * Pi ;

lat1 = latitude1 / 180 * Pi ;

lat2 = latitude2 / 180 * Pi ;

dlon = lon2 - lon1 ;

dlat = lat2 - lat1 ;

a = ( Sin ( dlat / 2 ) )^2 + Cos ( lat1 ) * Cos ( lat2 ) * ( Sin ( dlon / 2 ) )^2 ;

x = Sqrt ( 1 - a ) ;

y = Sqrt ( a ) ;

c =

Case (

x > 0 ; 2 * Atan ( y / x ) ;

x < 0 and y >= 0 ; 2 * Atan ( y / x ) + Pi ;

x < 0 and y < 0 ; 2 * Atan ( y / x ) - Pi ;

x = 0 and y > 0 ; Pi / 2 ;

x = 0 and y < 0 ; -Pi / 2

) ;

d = R * c ] ;

d

)

BEARING

Let ( [

lon1 = longitude1 / 180 * Pi ;

lon2 = longitude2 / 180 * Pi ;

lat1 = latitude1 / 180 * Pi ;

lat2 = latitude2 / 180 * Pi ;

dlon = lon2 - lon1 ;

dlat = lat2 - lat1 ;

x = Cos ( lat1 ) * Sin ( lat2 ) - Sin ( lat1 ) * Cos ( lat2 ) * Cos ( dlon );

y = Sin ( dlon ) * Cos ( lat2 ) ;

bear =

Case (

x > 0 ; Atan ( y / x ) ;

x < 0 and y >= 0 ; Atan ( y / x ) + Pi ;

x < 0 and y < 0 ; Atan ( y / x ) - Pi ;

x = 0 and y > 0 ; Pi / 2 ;

x = 0 and y < 0 ; -Pi / 2

)

] ;

bear / Pi * 180

)

• ###### 3. Re: Custom function for range and bearing between two geographical points

Excellent, thank you so much.