forked from gojekfarm/s2-calc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (102 loc) · 2.96 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"fmt"
"strconv"
"syscall/js"
"github.com/golang/geo/s2"
)
func main() {
calculateS2ID()
done := make(chan struct{})
calculateS2IDCallback := js.NewCallback(func(args []js.Value) {
calculateS2ID()
})
calculateLatLngCallback := js.NewCallback(func(args []js.Value) {
calculateLatLng()
})
js.Global().Get("document").
Call("getElementById", "latitude").
Call("addEventListener", "change", calculateS2IDCallback)
js.Global().Get("document").
Call("getElementById", "latitude").
Call("addEventListener", "keyup", calculateS2IDCallback)
js.Global().Get("document").
Call("getElementById", "longitude").
Call("addEventListener", "change", calculateS2IDCallback)
js.Global().Get("document").
Call("getElementById", "longitude").
Call("addEventListener", "keyup", calculateS2IDCallback)
js.Global().Get("document").
Call("getElementById", "level").
Call("addEventListener", "change", calculateS2IDCallback)
js.Global().Get("document").
Call("getElementById", "level").
Call("addEventListener", "keyup", calculateS2IDCallback)
js.Global().Get("document").
Call("getElementById", "s2id").
Call("addEventListener", "change", calculateLatLngCallback)
js.Global().Get("document").
Call("getElementById", "s2id").
Call("addEventListener", "keyup", calculateLatLngCallback)
<-done
}
func calculateS2ID() {
latitudeStr := js.Global().Get("document").
Call("getElementById", "latitude").
Get("value").String()
longitudeStr := js.Global().Get("document").
Call("getElementById", "longitude").
Get("value").String()
levelStr := js.Global().Get("document").
Call("getElementById", "level").
Get("value").String()
if latitudeStr == "" || longitudeStr == "" || levelStr == "" {
return
}
latitude, err := strconv.ParseFloat(latitudeStr, 64)
if err != nil {
fmt.Println("Failed to parse latitude")
return
}
longitude, err := strconv.ParseFloat(longitudeStr, 64)
if err != nil {
fmt.Println("Failed to parse longitude")
return
}
level, err := strconv.Atoi(levelStr)
if err != nil {
fmt.Println("Failed to parse level")
return
}
s2ID := strconv.Itoa(int(s2.CellIDFromLatLng(s2.LatLngFromDegrees(latitude, longitude)).Parent(level)))
js.Global().Get("document").
Call("getElementById", "s2id").
Set("value", s2ID)
}
func calculateLatLng() {
s2idStr := js.Global().Get("document").
Call("getElementById", "s2id").
Get("value").String()
if s2idStr == "" {
return
}
s2idInt, err := strconv.Atoi(s2idStr)
if err != nil {
fmt.Println("Failed to parse s2id")
return
}
s2Cell := s2.CellID(s2idInt)
ll := s2Cell.LatLng()
latitude := fmt.Sprintf("%v", ll.Lat)
longitude := fmt.Sprintf("%v", ll.Lng)
level := s2Cell.Level()
js.Global().Get("document").
Call("getElementById", "latitude").
Set("value", latitude)
js.Global().Get("document").
Call("getElementById", "longitude").
Set("value", longitude)
js.Global().Get("document").
Call("getElementById", "level").
Set("value", level)
}