劲创营---任务项目
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

38 líneas
897 B

  1. package utils
  2. import (
  3. "math"
  4. )
  5. // 球面距离公式:https://baike.baidu.com/item/%E7%90%83%E9%9D%A2%E8%B7%9D%E7%A6%BB%E5%85%AC%E5%BC%8F/5374455?fr=aladdin
  6. // GeoDistance 计算地理距离,依次为两个坐标的纬度、经度、单位(默认:英里,K => 公里,N => 海里)
  7. func GeoDistance(lng1 float64, lat1 float64, lng2 float64, lat2 float64, unit ...string) float64 {
  8. const PI float64 = 3.141592653589793
  9. radlat1 := PI * lat1 / 180
  10. radlat2 := PI * lat2 / 180
  11. theta := lng1 - lng2
  12. radtheta := PI * theta / 180
  13. dist := math.Sin(radlat1)*math.Sin(radlat2) + math.Cos(radlat1)*math.Cos(radlat2)*math.Cos(radtheta)
  14. if dist > 1 {
  15. dist = 1
  16. }
  17. dist = math.Acos(dist)
  18. dist = dist * 180 / PI
  19. dist = dist * 60 * 1.1515
  20. if len(unit) > 0 {
  21. if unit[0] == "K" {
  22. dist = dist * 1.609344
  23. } else if unit[0] == "N" {
  24. dist = dist * 0.8684
  25. }
  26. }
  27. return dist
  28. }