-
Notifications
You must be signed in to change notification settings - Fork 3
/
coordinate.go
41 lines (28 loc) · 1.14 KB
/
coordinate.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
30
31
32
33
34
35
36
37
38
39
40
41
package communautofinder
// --------------------------------------------
// Compute on GPS position in latitude longitude
// --------------------------------------------
import "math"
type Coordinate struct {
latitude float64
longitude float64
}
func New(latitude float64, longitude float64) Coordinate {
return Coordinate{latitude: latitude, longitude: longitude}
}
func (baseCoordinate Coordinate) ExpandCoordinate(kilometers float64) (Coordinate, Coordinate) {
minCoordinate := baseCoordinate
maxCoordinate := baseCoordinate
minCoordinate.addKilometersToCoordinate(-kilometers)
maxCoordinate.addKilometersToCoordinate(kilometers)
return minCoordinate, maxCoordinate
}
func (coordinateToModify *Coordinate) addKilometersToCoordinate(kilometers float64) {
earthRadiusKm := 6371.0
latRad := coordinateToModify.latitude * math.Pi / 180.0
lonRad := coordinateToModify.longitude * math.Pi / 180.0
newLatRad := latRad + (kilometers / earthRadiusKm)
newLonRad := lonRad + (kilometers / (earthRadiusKm * math.Cos(latRad)))
coordinateToModify.latitude = newLatRad * 180.0 / math.Pi
coordinateToModify.longitude = newLonRad * 180.0 / math.Pi
}