Calculate the distance between two points on the earth based on the longitude and latitude. js implementation code

Source: Internet
Author: User

Using JS to calculate the distance between two points on the earth based on latitude and longitude
Recently, we used the formula for calculating the distance between two points on the earth's surface based on latitude and longitude, and then implemented it using JS.
There are two ways to calculate the distance between two points on the Earth's surface.

FirstBy default, The Earth is a smooth sphere, and The Distance between any two points is calculated. This Distance is called The Great Circle Distance ).
The formula is as follows:
Use JS to achieve:
Copy codeThe Code is as follows:
Var EARTH_RADIUS = 6378137.0; // unit: M
Var PI = Math. PI;

Function getRad (d ){
Return d * PI/180.0;
}

/**
* Caculate the great circle distance
* @ Param {Object} lat1
* @ Param {Object} lng1
* @ Param {Object} lat2
* @ Param {Object} lng2
*/
Function getGreatCircleDistance (lat1, lng1, lat2, lng2 ){
Var radLat1 = getRad (lat1 );
Var radLat2 = getRad (lat2 );

Var a = radLat1-radLat2;
Var B = getRad (lng1)-getRad (lng2 );

Var s = 2 * Math. asin (Math. sqrt (Math. pow (Math. sin (a/2), 2) + Math. cos (radLat1) * Math. cos (radLat2) * Math. pow (Math. sin (B/2), 2 )));
S = s * EARTH_RADIUS;
S = Math. round (s * 10000)/10000.0;

Return s;
}

This formula is correct in most cases. problems may occur only when dealing with relative points on the sphere,There is a formula for correction.Because there is no need, you can find it on the wiki.
Of course, we all know that the earth is not a real sphere, but an elliptical sphere, so we have the following formula:
Copy codeThe Code is as follows:
/**
* Approx distance between two points on earth ellipsoid
* @ Param {Object} lat1
* @ Param {Object} lng1
* @ Param {Object} lat2
* @ Param {Object} lng2
*/
Function getFlatternDistance (lat1, lng1, lat2, lng2 ){
Var f = getRad (lat1 + lat2)/2 );
Var g = getRad (lat1-lat2)/2 );
Var l = getRad (lng1-lng2)/2 );

Var sg = Math. sin (g );
Var sl = Math. sin (l );
Var sf = Math. sin (f );

Var s, c, w, r, d, h1, h2;
Var a = EARTH_RADIUS;
Var fl = 1/298. 257;

Sg = sg * sg;
Sl = sl * sl;
Sf = sf * sf;

S = sg * (1-sl) + (1-sf) * sl;
C = (1-sg) * (1-sl) + sf * sl;

W = Math. atan (Math. sqrt (s/c ));
R = Math. sqrt (s * c)/w;
D = 2 * w *;
H1 = (3 * r-1)/2/c;
H2 = (3 * r + 1)/2/s;

Return d * (1 + fl * (h1 * sf * (1-sg)-h2 * (1-sf) * sg ));
}

The result calculated by this formula is better than the first one. Of course, the longitude of the final result depends on the accuracy of the Input coordinates.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.