Recently used in accordance with the latitude and longitude calculation of the Earth's surface two distance between the formula, and then use JS to achieve a bit.
There are probably two ways to calculate the distance between two points on the Earth's surface.
The first is the default Earth is a smooth spherical surface, and then calculate the distance between any two points, this distance is called the Great Circle distance (the great Circle Distance).
The formula is as follows:
Use JS to achieve: 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, only when dealing with the relative points on the sphere, there is a problem, there is a modified formula, because there is no need to find out, can be found on the wiki.
Of course, we all know that the earth is not really a ball body, but ellipsoid, so with the following formula:
/* *
* Approx distance between two points on the 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 =