-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeo.go
More file actions
68 lines (64 loc) · 1.56 KB
/
Copy pathgeo.go
File metadata and controls
68 lines (64 loc) · 1.56 KB
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
package vivo
import (
"encoding/json"
"errors"
"io"
"strconv"
)
type geoPOIResponse struct {
Pois []POI `json:"pois"`
Total int `json:"total"`
}
type POI struct {
// 省
Province string `json:"province"`
// 区
District string `json:"district"`
// 市
City string `json:"city"`
// 经纬度坐标(02坐标)
Location string `json:"location"`
// 名称
Name string `json:"name"`
// 地址
Address string `json:"address"`
// 类型
Type string `json:"typeName"`
}
func (app *Vivo) GeoPOISearch(keywords string, city string, page int, page_size ...int) ([]POI, int, error) {
httpClient := app.newHttpClient()
httpClient.QueryParams.Add("keywords", keywords)
httpClient.QueryParams.Add("city", city)
httpClient.QueryParams.Add("page_num", strconv.Itoa(page))
if len(page_size) > 0 {
httpClient.QueryParams.Add("page_size", strconv.Itoa(page_size[0]))
}
httpRes, e := httpClient.Get("https://api-ai.vivo.com.cn/search/geo")
if e != nil {
return []POI{}, 0, e
}
defer httpRes.Body.Close()
body, e := io.ReadAll(httpRes.Body)
if e != nil {
return []POI{}, 0, e
}
if httpRes.StatusCode() != 200 {
resMap := make(map[string]interface{})
e := json.Unmarshal(body, &resMap)
if e != nil {
return []POI{}, 0, errors.New(string(body))
}
msg, ok := resMap["message"].(string)
if ok {
return []POI{}, 0, errors.New(msg)
}
msg, _ = resMap["msg"].(string)
return []POI{}, 0, errors.New(msg)
}
resData := geoPOIResponse{}
e = json.Unmarshal(body, &resData)
if e != nil {
return []POI{}, 0, e
}
return resData.Pois, resData.Total, nil
}