JS calculates a distance of two points based on latitude and longitude

Source: Internet
Author: User
Tags asin cos pow sin

Calculate distance based on two-point latitude and longitude
How are these longitude determined? The earth is constantly spinning around its axis (the earth's axis is a center of the north-south poles and the Earth.
Imaginary line), in the Earth's waist draw a large circle perpendicular to the axis, so that each point on the circle and the North and south poles of the same distance, this circle
It is called "the equator". At the north and south sides of the equator, many circles parallel to the equator are drawn, which are the "weft rings", the segments that make up these circles,
It's called Latitude. We set the equator at 0 degrees to the north and 90 degrees to the south, to the southern latitude south of the equator, to the north of the equator called latitude.
North Pole is latitude 90 degrees, Antarctica is south latitude 90 degrees. Latitude also marks the climate of hot and cold, such as the equator and low latitudes in the region without winter,
The polar and high latitudes have no summer, and the mid-latitude regions are distinct.
Second, from the North Pole to the Antarctic point, can draw many north-south direction and the Earth's equator vertical big circle, this is called "The Warp Circle";
The line of the circle is called the Warp. Year 1884, the International rules of the Greenwich Observatory in the suburbs of London
Longitude as the beginning of the calculation of latitude, that is, longitude zero 0 minutes and 0 seconds, also known as the "Prime Meridian." East of it for longitude, a total of 180 degrees;
West of it, for 180 degrees. Because the earth is round, the longitude of 180 degrees east longitude and longitude 180 degrees West is the same meridian line. Countries
Sat and public 180 degrees longitude is "International Date Line". In order to avoid the use of two different dates in the same area, the International Date Line is on land
Slightly deviated.
Each longitude and latitude can be subdivided into 60 points, each divided into 60 seconds and fractional seconds. Using longitude, we can determine
The exact location of every place on the Earth and show it on a map or globe. For example, what is the latitude and longitude of Beijing? We
It is easy to find out from the map is 116 degrees east longitude 24 points, north latitude 39 degrees 54 points. Vessels sailing in the sea, as long as the longitude of the location is measured
, you can determine the position and direction of the ship in the ocean. Latitude has a total of 90 degrees. The equator is 0 degrees, aligned to the Poles, the smaller the circle,
The greater the degree.
The horizontal line is latitude, and the vertical line is longitude.
Of course, we can calculate the equation of four yuan and two times.
Both longitude and latitude are an angle. Longitude is a two-sided angle, which is the angle of two meridians. Because all meridians are the same length, in order to measure
Longitude Select a starting point and, after consultation with the 1884 International Conference, decides to pass the Greenwich Royal city of Thames, R. on the south Bank of London.
The meridian of the Cross of a main meridian instrument of the Observatory (former site) is the beginning meridian, called the Prime Meridian. The Prime Meridian plane is
The point face is the local meridian plane. The longitude of a point is the angle between the meridian plane and the Prime Meridian plane. In
Measured on the equator, from the Prime Meridian plane as the starting point, respectively east to West Measurement, the east measure value is called the longitude degree, the west measure value is called the West
Longitude. Thus, the longitude of the ground is the direction and angular distance of the prime meridian. The Prime meridian is the 0° longitude, the highest longitude degree
The large value is 180°, the maximum longitude is 180°, the east and west longitude 180 ° meridian is the same meridian, so regardless of longitude or west longitude, and collectively
180° longitude.
Latitude is a line face angle. The starting plane is the equatorial plane, and the line is the local ground normal. The so-called normal, that is perpendicular to the reference flat sphere surface of the line.
The latitude of a place is the angle between the normal of the ground and the equatorial plane. Latitude is measured on the local meridian, measured from the equator to the south, north to north
The measure is called the northern latitude, and the southward measure is called the latitude degree. Thus, the latitude of a land is the direction and angular distance of the equator. Equator is
0° latitude, the maximum value of northern latitude is 90°, that is, the North Pole, the maximum latitude is 90°, that is, the Antarctic point.

Interchange by latitude

Degrees (DDD): E 108.90593 degrees N 34.2163 degrees

How to convert degrees (DDD):: 108.90593 degrees to degrees seconds (DMS) Longitude e 108 degrees 54 minutes 22.2 seconds? The conversion method is to change the 108.90593 integer digits to 108 (degrees), use 0.90593*60=54.3558, take integer digits 54 (min), The 0.3558*60=21.348 is then rounded to 21 (seconds), which translates to 108 degrees 54 minutes and 21 seconds.

The same will be the degree of seconds (DMS): East longitude e 108 degrees 54 minutes 22.2 seconds conversion into degrees (DDD) method is as follows: 108 degrees 54 minutes 22.2 seconds =108+ (54/60) + (22.2/3600) = 108.90616 degrees

Because of the reason that the decimal digit is retained in the calculation, there is a certain error in the positive and negative calculation, but the error effect is not great. The 1-second error is a few meters. GPS car Friends can use the above method to convert to their own desired unit coordinates.

Converted to meters by latitude

Latitude is divided into 60 points, each divided into 60 seconds and fractional seconds.

The latitude line projects seemingly horizontal parallel lines on the graph, but is actually a circle of different radii. All locations with the same specific latitude are on the same parallel.
The equatorial latitude is 0°, dividing the planets into the southern and northern hemispheres.
Latitude refers to the line angle between a point and the Earth's spherical surface and the equatorial plane of the earth, and its value is between 0-90 degrees. The latitude of the point north of the equator is called Latitude, which is recorded as N, and the latitude of the point south of the equator is said to be S.
Latitude values in the region between 0-30 degrees called the low latitude region, the latitude value in the area between 30-60 degrees is called the middle latitude region, the latitude value in the area between 60-90 degrees is called the high latitude region.
The equator, the southern Tropic, the Tropic of Cancer, the Antarctic Circle and the Arctic Circle are special parallels.
Latitude 1 seconds in length
The total meridian length of the earth is about 40008km. Average:
Latitude 1 degrees = approximately 111km
Latitude 1 min = approximately 1.85km
Latitude 1 sec = approximately 30.9m

Calculates the distance between two points based on the latitude and longitude of any two points on Earth.

The earth is a nearly standard ellipsoid with an equatorial radius of 6378.140 km, a polar radius of 6356.755 km and an average radius of 6371.004 km. If we assume that the earth is a perfect sphere, then its radius is the average radius of the Earth, recorded as R. If the 0-degree meridian is used as a benchmark, the surface distance between the two points can be calculated based on the latitude and longitude of any two points on the Earth's surface (this ignores the error caused by the Earth's surface topography, which is only a theoretical estimate). The latitude and longitude of 1th A is (Lona, LatA), the latitude and longitude of 2nd B is (LONB, LATB), according to the datum of 0 degrees longitude, the positive value of longitude (longitude), the longitude is negative (-longitude), north latitude takes 90-latitude value (90- Latitude), 90+ latitude value (90+latitude), the two points after the above treatment are counted as (Mlona, Mlata) and (Mlonb, MLATB). Then, according to triangular derivation, we can get the following formula to calculate the distance of two points:

C = sin (mlata) *sin (MLATB) *cos (mlona-mlonb) + cos (mlata) *cos (MLATB)

Distance = R*arccos (C) *pi/180

Here, R and distance units are the same, if the use of 6371.004 km as a radius, then distance is the unit, if you want to use other units, such as mile, also need to do unit conversion, 1-kilometer =0.621371192mile

If the longitude is treated only as positive or negative, and the latitude is not 90-latitude (assuming that the northern hemisphere is the only application in the southern hemisphere), then the formula will be:

C = sin (LatA) *sin (LATB) + cos (LatA) *cos (LATB) *cos (MLONA-MLONB)

Distance = R*arccos (C) *pi/180

The above can be introduced through a simple triangular transformation.

If the input and output of trigonometric functions are in radians, then the formula can also be written:

C = sin (lata*pi/180) *sin (latb*pi/180) + cos (lata*pi/180) *cos (latb*pi/180) *cos ((MLONA-MLONB) *pi/180)

Distance = R*arccos (C) *pi/180

That is

C = sin (lata/57.2958) *sin (latb/57.2958) + cos (lata/57.2958) *cos (latb/57.2958) *cos ((MLONA-MLONB)/57.2958)

Distance = R*arccos (c) = 6371.004*arccos (c) kilometer = 0.621371192*6371.004*arccos (c) mile = 3958.758349716768*arccos (c ) Mile

In practical application, it is generally through an individual zip code to find the corresponding regional center of latitude and longitude, and then based on these latitude and longitude to calculate the distance between each other, so as to estimate the approximate distance between certain groups of the range ( For example, the distribution of hotel passengers-the latitude and longitude of each traveler's postal code and the distance range computed by the latitude and longitude of the hotel-and so on, so a database is a useful resource for querying latitude and longitude by postal code.
Attached: C # code:

Private Const Double Earth_radius = 6378.137;//Earth radius
private static Double rad (double D)
{
return d * math.pi/180.0;
}

public static double Getdistance (double lat1, double lng1, double lat2, double lng2)
{
Double RADLAT1 = rad (LAT1);
Double radLat2 = rad (LAT2);
Double A = RADLAT1-RADLAT2;
Double b = rad (lng1)-Rad (LNG2);

Double 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;
return s;
}

The recent use of the latitude and longitude to calculate the Earth's surface distance between two points of the formula, and then use JS to achieve a bit.

There are probably two ways to calculate the distance between two points of the Earth's surface.

The first is that the default Earth is a smooth sphere and then calculates the distance between any two points, which is called the Great Circle distance (the Great circle Distance).

The formula is as follows:

Use JS to achieve the following:

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 more correct in most cases, only when dealing with the relative points on the spherical surface, there will be problems, 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 actually a real sphere, but an ellipsoid, so the following formula is available:


/**
* Approx distance between, 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));
}

This formula calculates a better result than the first, and of course, the longitude of the final result actually depends on the accuracy of the incoming coordinates.

JS calculates a distance of two points based on latitude and longitude

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.