/
google_location.go
57 lines (42 loc) · 1.16 KB
/
google_location.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
package eyego
import (
"net/http"
"fmt"
"encoding/json"
"bytes"
"io/ioutil"
"errors"
)
var (
LOC_BASE_URL = "https://www.googleapis.com/geolocation/v1/geolocate"
)
type WifiAccessPoints struct {
AccessPoints []AccessPointSightingInfo `json:"wifiAccessPoints"`
ConsiderIp bool `json:"considerIp"`
}
type Location struct {
Latitude float64 `json:"lat"`
Longitude float64 `json:"lng"`
}
type LocationResult struct {
Location Location `json:"location"`
Accuracy float64 `json:"accuracy"`
}
func GPSCoordinates(aps []AccessPointSightingInfo) (lr LocationResult, err error) {
req, err := json.Marshal(WifiAccessPoints{AccessPoints:aps,ConsiderIp:false})
if err != nil { return }
resp, err := http.Post(fmt.Sprintf("%s?key=%s", LOC_BASE_URL, Config().GoogleAPIKey), "application/json", bytes.NewBuffer(req))
if err != nil { return }
if resp.StatusCode != 200 {
body, _ := ioutil.ReadAll(resp.Body)
err = errors.New(fmt.Sprintf("Request failed: %s", body))
return
}
defer resp.Body.Close()
lr = LocationResult{}
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil { return }
err = json.Unmarshal(bytes, &lr)
if err != nil { return }
return
}