-
Notifications
You must be signed in to change notification settings - Fork 1
/
getlocation.go
executable file
·109 lines (94 loc) · 2.18 KB
/
getlocation.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
package main
import (
"fmt"
"github.com/maltegrosse/go-geoclue2"
"log"
"os"
)
func main() {
// create new Instance of Geoclue Manager
gcm, err := geoclue2.NewGeoclueManager()
if err != nil {
log.Fatal(err.Error())
}
// req availableAccuracyLevel
aAccuracyLevel, err := gcm.GetAvailableAccuracyLevel()
if err != nil {
log.Fatal(err.Error())
}
fmt.Println("Available Accuracy Level: ", aAccuracyLevel)
// create new Instance of Geoclue Client
client, err := gcm.GetClient()
if err != nil {
log.Fatal(err.Error())
}
// desktop id is required to start the client
// (double check your geoclue.conf file)
err = client.SetDesktopId("firefox")
if err != nil {
log.Fatal(err.Error())
}
// Set RequestedAccuracyLevel
err = client.SetRequestedAccuracyLevel(geoclue2.GClueAccuracyLevelExact)
if err != nil {
log.Fatal(err.Error())
}
// Get RequestedAccuracyLevel
level, err := client.GetRequestedAccuracyLevel()
if err != nil {
log.Fatal(err.Error())
}
fmt.Println("Requested Accuracy Level: ", level)
// client must be started before requesting the location
err = client.Start()
if err != nil {
log.Fatal(err.Error())
}
// create new Instance of Geoclue Location
location, err := client.GetLocation()
if err != nil {
log.Fatal(err.Error())
}
// get latitude
latitude, err := location.GetLatitude()
if err != nil {
log.Fatal(err.Error())
}
fmt.Println(latitude)
// get longitude
longitude, err := location.GetLongitude()
if err != nil {
log.Fatal(err.Error())
}
fmt.Println(longitude)
/* // get accuracy
accuracy, err := location.GetAccuracy()
if err != nil {
log.Fatal(err.Error())
}
fmt.Println(accuracy)*/
// listen location updates
c := client.SubscribeLocationUpdated()
for v := range c {
fmt.Println(v)
oldl, newl, err := client.ParseLocationUpdated(v)
if err != nil {
log.Fatal(err.Error())
}
fmt.Println("old")
oldjson, err := oldl.MarshalJSON()
if err != nil {
log.Fatal(err.Error())
}
fmt.Println(string(oldjson))
fmt.Println("---")
fmt.Println("new")
newjson, err := newl.MarshalJSON()
if err != nil {
log.Fatal(err.Error())
}
fmt.Println(string(newjson))
fmt.Println("---")
}
os.Exit(0)
}