Home  >  Article  >  Web Front-end  >  Javascript calculates the distance between two places by longitude and latitude

Javascript calculates the distance between two places by longitude and latitude

不言
不言Original
2018-09-10 14:40:538034browse

How to calculate the distance between two places in javascript? I believe there are many ways to achieve this. The following article will introduce you to JavaScript to calculate the distance between two places through longitude and latitude.

Recently, due to work needs, I searched online for methods to calculate the distance between two places based on longitude and latitude. I found that it was either a geometric method, drawing a picture, making a bunch of auxiliary lines, and then proving the reasoning, or just applying it without saying anything. formula. This article introduces an easy-to-understand way to find this distance.

Thoughts

The earth is an irregular ellipsoid. For simplicity, we treat it as a sphere for calculation.
The shortest distance between two places on a sphere is the length of the minor arc of a great circle passing through two points.

The idea is as follows:

Arc length ← Chord length (distance between two points) ← Two point coordinates (Rectangular coordinates) ← Latitude and longitude

Calculation

1. Coordinate conversion

Suppose:

The radius of the earth is $R$

The line connecting the center of the earth to E 0° N 0° is the x-axis

The line connecting the center of the earth to E 90° N 0° is the y-axis

The line connecting the center of the earth to E 0° N 90° is the z-axis

There is a point $A on the surface of the earth $, the longitude is $e$, the latitude is $n$, and the unit is radians

Then the coordinates of $A$ can be expressed as:

$$ x = R \cdot cos(n) \cdot cos(e)\\ y = R \cdot cos(n) \cdot sin(e)\\ z = R \cdot sin(n) $$

Code

const R = 6371
const {cos, sin, PI} = Math

let getPoint = (e, n) => {
    //首先将角度转为弧度
    e *= PI/180
    n *= PI/180
    reutrn {
        x: R*cos(n)*cos(e),
        y: R*cos(n)*sin(e),
        z: R*sin(n)
    }
}

2. Calculate the distance between two points based on coordinates

This is too simple, skip

3. Find the arc length based on the string length

You can draw a diagram to help understand:

Javascript calculates the distance between two places by longitude and latitude

The chord length $c$ is now known , radius $R$, requires the length of arc $r$
This is very simple, just find the size of $∠\alpha$ first:

$$ \alpha = \arcsin(c/2/R)\\ r = 2\alpha \cdot R $$

Code

const {asin} = Math
const R = 6371

r = asin(c/2/R)*2*R

Final code

/**
 * 获取两经纬度之间的距离
 * @param {number} e1 点1的东经, 单位:角度, 如果是西经则为负
 * @param {number} n1 点1的北纬, 单位:角度, 如果是南纬则为负
 * @param {number} e2
 * @param {number} n2
 */
function getDistance(e1, n1, e2, n2){
    const R = 6371
    const { sin, cos, asin, PI, hypot } = Math
    
    /** 根据经纬度获取点的坐标 */
    let getPoint = (e, n) => {
        e *= PI/180
        n *= PI/180
        //这里 R* 被去掉, 相当于先求单位圆上两点的距, 最后会再将这个距离放大 R 倍
        return {x: cos(n)*cos(e), y: cos(n)*sin(e), z: sin(n)}
    }
    
    let a = getPoint(e1, n1)
    let b = getPoint(e2, n2)
    let c = hypot(a.x - b.x, a.y - b.y, a.z - b.z)
    let r = asin(c/2)*2*R
    return r
}

Related recommendations:

Calculate the distance between two places based on longitude and latitude

PHP calculates the distance based on the longitude and latitude between two points, PHP calculates the longitude and latitude of two points

The above is the detailed content of Javascript calculates the distance between two places by longitude and latitude. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn