-
Notifications
You must be signed in to change notification settings - Fork 12
/
gaode.go
69 lines (58 loc) · 1.44 KB
/
gaode.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
package main
import (
"errors"
"fmt"
log "github.com/liudanking/goutil/logutil"
"github.com/liudanking/goutil/netutil"
)
const (
// Oops, for temporary usage
GaodeKey = "YOUR_KEY"
)
type RegeoRsp struct {
Status string `json:"status"`
Info string `json:"info"`
Infocode string `json:"infocode"`
Regeocode struct {
AddressComponent struct {
Country string `json:"country"`
Province string `json:"province"`
City interface{} `json:"city"`
} `json:"addressComponent"`
} `json:"regeocode"`
}
func (rsp *RegeoRsp) GetCity() string {
city, ok := rsp.Regeocode.AddressComponent.City.(string)
if ok && city != "" {
return city
}
// 直辖市
if rsp.Regeocode.AddressComponent.Province != "" {
return rsp.Regeocode.AddressComponent.Province
}
return "未知"
}
func GetRegeoInfo(lng string, lat string) (*RegeoRsp, error) {
addr := "http://restapi.amap.com/v3/geocode/regeo"
params := map[string]interface{}{
"key": GaodeKey,
"location": fmt.Sprintf("%s,%s", lng, lat),
}
rsp := &RegeoRsp{}
_, err := netutil.DefaultHttpClient().RequestForm("GET", addr, params).DoJSON(rsp)
if err != nil {
return nil, err
}
if rsp.Status == "0" {
return nil, errors.New(rsp.Info)
}
return rsp, nil
}
func GetCityByPosition(lng, lat string) string {
rsp, err := GetRegeoInfo(lng, lat)
if err != nil {
log.Warning("get regeo [%s, %s] failed:%v", lng, lat, err)
return "未知"
}
return rsp.GetCity()
}