Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Distance of two points on the terrestrial sphere (distance in km, azimut in °)
- var result = distance({
- lon1: 7.34684,
- lat1: 47.36376,
- lon2: 7.60884,
- lat2: 47.52812
- });
- console.log(result);
- function distance({ lon1, lon2, lat1, lat2}) {
- let azimut, dist;
- const RADIANS = 0.017453292519943295769236907684886; // = π / 180°
- const NAUTICAL_MILE = 1.852216; // = 1′ of arc in km
- lon1 = lon1 * RADIANS;
- lon2 = lon2 * RADIANS;
- lat1 = lat1 * RADIANS;
- lat2 = lat2 * RADIANS;
- // Compute the distance arc of distance using the cosine law for spherical triangles
- let a = Math.acos(
- Math.sin( lat1 ) * Math.sin( lat2 ) +
- Math.cos( lat1 ) * Math.cos( lat2 ) * Math.cos( lon1 - lon2 ) );
- // Shortest distance in km
- dist = a * 60 * NAUTICAL_MILE / RADIANS;
- // Azimut
- azimut = a < 1e-4 ? 0 :
- Math.atan2(
- Math.cos( lat1 ) * Math.sin( lon2-lon1 ),
- Math.sin( lat2 ) * Math.cos( lat1 ) - Math.cos( lon2-lon1 ) * Math.cos( lat2 ) * Math.sin( lat1 )
- ) / RADIANS;
- if (azimut < 0) azimut += 360;
- return {azimut,dist};
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement