forked from strava/go.strava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polyline.go
48 lines (37 loc) · 862 Bytes
/
polyline.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
42
43
44
45
46
47
48
package strava
type Polyline string
// Decode will take the polyline which is a string
// in standard Google polyline encoding and convert it to an array.
func (p Polyline) Decode() [][2]float64 {
var count, index int
factor := 1.0e5
line := make([][2]float64, 0)
tempLatLng := [2]int{0, 0}
for index < len(p) {
var result int
var b int = 0x20
var shift uint
for b >= 0x20 {
b = int(p[index]) - 63
index++
result |= (b & 0x1f) << shift
shift += 5
}
// sign dection
if result&1 != 0 {
result = ^(result >> 1)
} else {
result = result >> 1
}
if count%2 == 0 {
result += tempLatLng[0]
tempLatLng[0] = result
} else {
result += tempLatLng[1]
tempLatLng[1] = result
line = append(line, [2]float64{float64(tempLatLng[0]) / factor, float64(tempLatLng[1]) / factor})
}
count++
}
return line
}