Using JS to calculate the distance between two points on the earth based on latitude and longitude

Source: Internet
Author: User
Tags cos pow sin

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 the following:
Code highlighting produced by Actipro Codehighlighter (freeware)

--> 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:
Code highlighting produced by Actipro Codehighlighter (freeware)

/* *
* 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 = 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 formula calculates the result to be better than the first, and of course the longitude of the final result is actually dependent on the precision of the incoming coordinates.

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: 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.