-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathclient.go
91 lines (70 loc) · 2.25 KB
/
client.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
// Created by @menduo @ 2019-01-24
package gobaidumap
import (
"fmt"
"errors"
)
// BaiduMapClient
type BaiduMapClient struct {
ak string
}
// NewBaiduMapClient 新建一个
func NewBaiduMapClient(ak string) *BaiduMapClient {
return &BaiduMapClient{ak: ak}
}
// GetAk 获取 ak
func (bc *BaiduMapClient) GetAk() string {
return bc.ak
}
// SetAk 设置 ak
func (bc *BaiduMapClient) SetAk(ak string) {
bc.ak = ak
}
// GetAddressViaGEO 通过 GEO 坐标信息获取地址
func (bc *BaiduMapClient) GetAddressViaGEO(lat, lng string) (*StructGEOToAddress, error) {
res := new(StructGEOToAddress)
parameter := fmt.Sprintf("&location=%s,%s&output=json&pois=0", lat, lng)
reqURL := fmt.Sprintf("%s%s%s", reqURLForGEO, bc.GetAk(), parameter)
res2, err := requestBaidu("GetAddressViaGEO", reqURL)
if err != nil {
return res, err
}
if res2.(*StructGEOToAddress).Status != 0 {
message := fmt.Sprintf("百度 API 报错:%s", res2.(*StructGEOToAddress).Msg)
return res, errors.New(message)
}
res3 := res2.(*StructGEOToAddress)
return res3, nil
}
// GetGeoViaAddress 通过地址获得 GEO 坐标
func (bc *BaiduMapClient) GetGeoViaAddress(address string) (*StructAddressToGEO, error) {
res := new(StructAddressToGEO)
parameter := fmt.Sprintf("&address=%s&output=json&pois=1", address)
reqURL := fmt.Sprintf("%s%s%s", reqURLForGEO, bc.GetAk(), parameter)
res2, err := requestBaidu("GetGeoViaAddress", reqURL)
if err != nil {
return res, err
}
if res2.(*StructAddressToGEO).Status != 0 {
message := fmt.Sprintf("百度 API 报错:%s", res2.(*StructAddressToGEO).Msg)
return res, errors.New(message)
}
res3 := res2.(*StructAddressToGEO)
return res3, nil
}
// GetAddressViaIP 通过 IP 获取地址
func (bc *BaiduMapClient) GetAddressViaIP(address string) (*StructIPToAddress, error) {
res := new(StructIPToAddress)
parameter := fmt.Sprintf("&ip=%s&output=json&pois=0", address)
reqURL := fmt.Sprintf("%s%s%s", reqURLForIP, bc.GetAk(), parameter)
res2, err := requestBaidu("GetAddressViaIP", reqURL)
if err != nil {
return res, err
}
if res2.(*StructIPToAddress).Status != 0 {
message := fmt.Sprintf("百度 API 报错:%s", res2.(*StructIPToAddress).Message)
return res, errors.New(message)
}
res3 := res2.(*StructIPToAddress)
return res3, nil
}