Calculate the distance between two points on the earth based on longitude and latitude using JS
Recently I used the formula to calculate the distance between two points on the earth's surface based on longitude and latitude, and then implemented it using JS.
There are roughly two ways to calculate the distance between two points on the earth's surface.
The first is to assume that the earth is a smooth spherical surface, and then calculate the distance between any two points. This distance is called the Great Circle Distance.
The formula is as follows:
Use JS to implement it as:
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 will arise only when dealing with relative points on the sphere.
There is a modified formula , but it was not found because there is no need. You can check it on the wiki.
Of course, we all know that the earth is not really a sphere, but an ellipsoid, so we have the following formula:
/**
* 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*a;
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 is actually better. Depends on the accuracy of the coordinates passed in.