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.

35 líneas
1.0KB

  1. package main
  2. import "math"
  3. // haversin(θ) function
  4. func hsin(theta float64) float64 {
  5. return math.Pow(math.Sin(theta/2), 2)
  6. }
  7. // Distance function returns the distance (in meters) between two points of
  8. // a given longitude and latitude relatively accurately (using a spherical
  9. // approximation of the Earth) through the Haversin Distance Formula for
  10. // great arc distance on a sphere with accuracy for small distances
  11. //
  12. // point coordinates are supplied in degrees and converted into rad. in the func
  13. //
  14. // distance returned is METERS!!!!!!
  15. // http://en.wikipedia.org/wiki/Haversine_formula
  16. func Distance(lat1, lon1, lat2, lon2 float64) float64 {
  17. // convert to radians
  18. // must cast radius as float to multiply later
  19. var la1, lo1, la2, lo2, r float64
  20. la1 = lat1 * math.Pi / 180
  21. lo1 = lon1 * math.Pi / 180
  22. la2 = lat2 * math.Pi / 180
  23. lo2 = lon2 * math.Pi / 180
  24. r = 6378100 // Earth radius in METERS
  25. // calculate
  26. h := hsin(la2-la1) + math.Cos(la1)*math.Cos(la2)*hsin(lo2-lo1)
  27. return 2 * r * math.Asin(math.Sqrt(h))
  28. }