-
Notifications
You must be signed in to change notification settings - Fork 0
/
baidumap_json_xml.go
122 lines (107 loc) · 2.95 KB
/
baidumap_json_xml.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
package main
import (
"net/http"
"io/ioutil"
"fmt"
"encoding/json"
"net/url"
)
func main() {
hc := http.Client{}
resp, err := hc.Get("http://api.map.baidu.com/place/v2/search?query=ATM机&tag=银行®ion=北京&output=json&ak=fGn7DfcwOOoGhl9U2EGuCfjKR0VMKctI")
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
}
fmt.Println(string(body))
//res := &StructAddressToGEO{}
//xml.Unmarshal(body, res)
resj := &StructAddressToGEOJ{}
json.Unmarshal(body, resj)
resj2 := &AutoGenerated{}
json.Unmarshal(body, resj2)
//fmt.Println(res)
fmt.Println(resj)
fmt.Println(resj2)
u, _ := url.Parse("http://api.map.baidu.com/place/v2/search")
q := u.Query()
q.Set("query", "ATM机")
q.Set("tag", "银行")
q.Set("region", "北京")
q.Set("output", "json")
q.Set("ak", "fGn7DfcwOOoGhl9U2EGuCfjKR0VMKctI")
u.RawQuery = q.Encode()
fmt.Println(u)
res, _ := http.Get(u.String())
result, err := ioutil.ReadAll(res.Body)
res.Body.Close()
fmt.Println(string(result)== string(body))
//fmt.Println(u.Host+ u.Path+u.RawQuery)
//fmt.Println(strings.Compare(string(u.Path), "http://api.map.baidu.com/place/v2/search?query=ATM机&tag=银行®ion=北京&output=json&ak=fGn7DfcwOOoGhl9U2EGuCfjKR0VMKctI"))
//fmt.Println(q)
}
type StructAddressToGEO struct {
//XMLName xml.Name `xml:"PlaceSearchResponse"`
Status int `xml:"status"`
Results []Result `xml:"results>result"`
Msg string `xml:"message"`
}
type Result struct {
//XMLName xml.Name `xml:"result"`
Name string `xml:"name"`
Location Locate `xml:"location"`
Province string `xml:"province"`
Address string `xml:"address"`
City string `xml:"city"`
Area string `xml:"area"`
Detail string `xml:"detail"`
Uid string `xml:"uid"`
}
type Locate struct {
Lng float64 `xml:"lng"`
Lat float64 `xml:"lat"`
}
type StructAddressToGEOJ struct {
//XMLName xml.Name `xml:"PlaceSearchResponse"`
Status int `json:"status"`
Results []ResultJ `json:"results"`
Msg string `json:"message"`
}
type ResultJ struct {
//XMLName xml.Name `xml:"result"`
Name string `json:"name"`
Location LocateJ `json:"location"`
Address string `json:"address"`
Province string `json:"province"`
City string `json:"city"`
Area string `json:"area"`
Detail int `json:"detail"`
UID string `json:"uid"`
StreetID string `json:"street_id,omitempty"`
}
type LocateJ struct {
Lng float64 `json:"lng"`
Lat float64 `json:"lat"`
}
type AutoGenerated struct {
Status int `json:"status"`
Message string `json:"message"`
Results []struct {
Name string `json:"name"`
Location struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"location"`
Address string `json:"address"`
Province string `json:"province"`
City string `json:"city"`
Area string `json:"area"`
Detail int `json:"detail"`
UID string `json:"uid"`
StreetID string `json:"street_id,omitempty"`
} `json:"results"`
}