GolangNote

Golang笔记

Golang 根据两点的经度和纬度计算两点之间的距离

Permalink

Golang 使用math 包根据两点的经度和纬度计算两点之间的距离

Golang 根据两点的经度和纬度计算两点之间的距离

Go: 计算两点间距离
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package main

import (
    "fmt"
    "math"
)

func main() {
    lat1 := 29.490295
    lng1 := 106.486654

    lat2 := 29.615467
    lng2 := 106.581515
    fmt.Println(EarthDistance(lat1, lng1, lat2, lng2))
}

func EarthDistance(lat1, lng1, lat2, lng2 float64) float64 {
    var radius float64 = 6371000 // 6378137
    rad := math.Pi / 180.0

    lat1 = lat1 * rad
    lng1 = lng1 * rad
    lat2 = lat2 * rad
    lng2 = lng2 * rad

    theta := lng2 - lng1
    dist := math.Acos(math.Sin(lat1)*math.Sin(lat2) + math.Cos(lat1)*math.Cos(lat2)*math.Cos(theta))
    return dist * radius
}

运行输出:

plaintext: 输出
1
16670.90427353956

本文网址: https://golangnote.com/topic/33.html 转摘请注明来源

Related articles

golang snappy 的使用场合

google 自家的 snappy 压缩优点是非常高的速度和合理的压缩率。压缩率比 gzip 小,CPU 占用小。...

Golang实现简单的Socks5代理

Socks5 代理较 `http/https` 代理有较好的性能,下面是借鉴某个著名开源软件的 local 实现的简单代理。...

Write a Comment to "Golang 根据两点的经度和纬度计算两点之间的距离"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.22.3 Processed in 0ms