import "math"
const EARTH_RADIUS = 6371000;
/**
* 此处是函数
*/
func get_two_points_distance(oldLL model.Longlati,newLL model.Longlati)(int){
lng1 :=oldLL.GetLong() * math.Pi/180
lat1 :=oldLL.GetLati() * math.Pi/180
lng2 :=newLL.GetLong() * math.Pi/180
lat2 :=newLL.GetLati() * math.Pi/180
calcLongitude := lng2-lng1
calcLatitude := lat2-lat1
stepOne := math.Pow(math.Sin(calcLatitude / 2), 2) + math.Cos(lat1) * math.Cos(lat2) * math.Pow(math.Sin(calcLongitude / 2), 2)
stepTwo := 2 * math.Asin(math.Min(1, math.Sqrt(stepOne)))
calculatedDistance := EARTH_RADIUS * stepTwo;
return int(calculatedDistance+0.5)
}
model部分
package model
import "strconv"
type Longlati struct {
Long float64
Lati float64
}
func (l *Longlati) GetLong() float64 {
return l.Long
}
func (l *Longlati) SetLong(value float64) {
l.Long = value
}
func (l *Longlati) SetLongFromString(value string) {
f,_ := strconv.ParseFloat(value, 64)
l.Long= f
}
func (l *Longlati) GetLati() float64 {
return l.Lati
}
func (l *Longlati) SetLati(value float64) {
l.Lati = value
}
func (l *Longlati) SetLatiFromString(value string) {
f,_ := strconv.ParseFloat(value, 64)
l.Lati = f
}