forked from go-admin-team/go-admin-core
/
amap.go
196 lines (168 loc) · 5.23 KB
/
amap.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package lbs
import (
"errors"
"fmt"
"github.com/spf13/cast"
"github.com/tidwall/gjson"
"github.com/xuanlingzi/go-admin-core/tools/utils"
"net/http"
"strings"
)
var _amap *http.Client
type Amap struct {
conn *http.Client
addr string
secretKey string
}
func GetAmapClient() *http.Client {
return _amap
}
func NewAmap(client *http.Client, addr, secretKey string) *Amap {
if client == nil {
client = &http.Client{
Transport: &http.Transport{},
}
_amap = client
}
c := &Amap{
conn: client,
addr: addr,
secretKey: secretKey,
}
return c
}
// Close 关闭连接
func (m *Amap) Close() {
if m.conn != nil {
m.conn.CloseIdleConnections()
m.conn = nil
}
}
func (*Amap) String() string {
return "amap"
}
func (m *Amap) GetAddress(latitude, longitude, radius float64) (map[string]string, error) {
/*
http://restapi.amap.com/v3/geocode/regeo?output=JSON
&location=" + longitude + "," + latitude + "
&key=
&radius=
&extensions=all
&batch=false
&roadlevel=0";
*/
url := fmt.Sprintf("%v/v3/geocode/regeo?output=JSON&key=%v&location=%v,%v&radius=%v&extensions=all&batch=false&roadlevel=0",
m.addr,
m.secretKey,
longitude,
latitude,
radius,
)
result := make(map[string]string)
addressInfo, err := utils.HttpGet(url)
if err != nil {
return result, err
}
if gjson.GetBytes(addressInfo, "status").Exists() && gjson.GetBytes(addressInfo, "status").Int() != 1 {
return result, errors.New(gjson.GetBytes(addressInfo, "info").String())
}
addr := ""
if gjson.GetBytes(addressInfo, "regeocode.addressComponent").Exists() {
result["country"] = gjson.GetBytes(addressInfo, "regeocode.addressComponent.country").String()
result["province"] = gjson.GetBytes(addressInfo, "regeocode.addressComponent.province").String()
addr += gjson.GetBytes(addressInfo, "regeocode.addressComponent.country").String()
addr += gjson.GetBytes(addressInfo, "regeocode.addressComponent.province").String()
if gjson.GetBytes(addressInfo, "regeocode.addressComponent.city").Exists() {
city := gjson.GetBytes(addressInfo, "regeocode.addressComponent.city").String()
if city != "[]" {
result["city"] = city
addr += city
}
}
result["district"] = gjson.GetBytes(addressInfo, "regeocode.addressComponent.district").String()
result["township"] = gjson.GetBytes(addressInfo, "regeocode.addressComponent.township").String()
addr += gjson.GetBytes(addressInfo, "regeocode.addressComponent.district").String()
addr += gjson.GetBytes(addressInfo, "regeocode.addressComponent.township").String()
if gjson.GetBytes(addressInfo, "regeocode.addressComponent.streetNumber").Exists() {
streetNumber := fmt.Sprintf("%v%v",
gjson.GetBytes(addressInfo, "regeocode.addressComponent.streetNumber.street").String(),
gjson.GetBytes(addressInfo, "regeocode.addressComponent.streetNumber.number").String(),
)
addr += streetNumber
result["street"] = streetNumber
}
}
if gjson.GetBytes(addressInfo, "regeocode.formatted_address").Exists() {
result["address"] = gjson.GetBytes(addressInfo, "regeocode.formatted_address").String()
} else {
result["address"] = addr
}
return result, nil
}
func (m *Amap) GetCoordinate(keyword string) (longitude float32, latitude float32, address string, err error) {
/*
http://restapi.amap.com/v3/geocode/geo?output=JSON
&address=" + address;
*/
url := fmt.Sprintf("%v/v3/geocode/geo?output=JSON&key=%v&address=%v",
m.addr,
m.secretKey,
keyword,
)
addressInfo, err := utils.HttpGet(url)
if err != nil {
return
}
if gjson.GetBytes(addressInfo, "status").Exists() && gjson.GetBytes(addressInfo, "status").Int() != 1 {
err = errors.New(gjson.GetBytes(addressInfo, "info").String())
return
}
if gjson.GetBytes(addressInfo, "geocodes.#").Int() > 0 {
address = gjson.GetBytes(addressInfo, "geocodes.0.formatted_address").String()
loc := gjson.GetBytes(addressInfo, "geocodes.0.location").String()
longitude = cast.ToFloat32(loc[:strings.Index(loc, ",")])
latitude = cast.ToFloat32(loc[strings.Index(loc, ",")+1:])
}
return
}
func (m *Amap) GetPosition(imei, network, ac, ci, snr string, result *map[string]interface{}) error {
/*
http://apilocate.amap.com/position?output=JSON
&key=%v
&accesstype=0
&imei=" + imei + "
&cdma=0
&network=" + network + "
&bts=460,0," + tac + "," + cellId + "," + snr
*/
url := fmt.Sprintf("%v/position?output=JSON&key=%v&accesstype=0&imei=%v&cdma=0&network=%v&bts=460,0,%v,%v,%v",
m.addr,
m.secretKey,
imei,
network,
ac,
ci,
snr,
)
addressInfo, err := utils.HttpGet(url)
if err != nil {
return fmt.Errorf("GetPosition error %v", err)
}
if gjson.GetBytes(addressInfo, "info").Exists() {
status := gjson.GetBytes(addressInfo, "info").String()
if strings.EqualFold(status, "OK") == false {
return fmt.Errorf("GetPosition error %v", addressInfo)
}
}
if gjson.GetBytes(addressInfo, "geocodes.#").Int() > 0 {
(*result)["address"] = gjson.GetBytes(addressInfo, "result.desc").String()
loc := gjson.GetBytes(addressInfo, "result.location").String()
(*result)["longitude"] = cast.ToFloat32(loc[:strings.Index(loc, ",")])
(*result)["latitude"] = cast.ToFloat32(loc[strings.Index(loc, ",")+1:])
}
return nil
}
// GetClient 暴露原生client
func (m *Amap) GetClient() interface{} {
return m.conn
}