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

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

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.