Created
August 6, 2017 01:39
-
-
Save utopiaprince/4d739a2ec6ca1b29e0f237138c91b02f to your computer and use it in GitHub Desktop.
go计算提供经纬度的两点距离
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package compute | |
import ( | |
"math" | |
) | |
// 地球半径,单位米 | |
const R = 6367000 | |
// lonA, latA分别为A点的纬度和经度 | |
// lonB, latB分别为B点的纬度和经度 | |
// 返回的距离单位为米 | |
func Sphere(lonA, latA, lonB, latB float64) float64 { | |
c := math.Sin(latA)*math.Sin(latB)*math.Cos(lonA-lonB) + math.Cos(latA)*math.Cos(latB) | |
return R * math.Acos(c) * math.Pi / 180 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
我有點好奇,這裏的 6367000是怎麼來的?我看了不少文章都有這個數值。但是在 wiki 上https://zh.wikipedia.org/wiki/%E5%9C%B0%E7%90%83%E5%8D%8A%E5%BE%84#地球的模型和半径 或者百度百科上 https://baike.baidu.com/item/%E5%9C%B0%E7%90%83%E5%8D%8A%E5%BE%84 都是 6371.