2022-01-16 20:47:39 +00:00
|
|
|
package baidu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
2022-03-12 18:00:40 +00:00
|
|
|
type baiduMap struct {
|
2022-01-16 20:47:39 +00:00
|
|
|
Ak string
|
|
|
|
Url string
|
|
|
|
}
|
|
|
|
|
2022-03-12 18:00:40 +00:00
|
|
|
var BaiDuMap = baiduMap{}
|
|
|
|
|
|
|
|
func (that *baiduMap) Init(Ak string) {
|
2022-01-16 20:47:39 +00:00
|
|
|
//"ak=ZeT902EZvVgIoGVWEFK3osUm"
|
2022-03-12 17:48:54 +00:00
|
|
|
that.Ak = Ak
|
|
|
|
that.Url = "https://api.map.baidu.com/place/v2/suggestion?output=json" + "&ak=" + Ak
|
2022-01-16 20:47:39 +00:00
|
|
|
//query
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPosition 获取定位列表
|
2022-03-12 18:00:40 +00:00
|
|
|
func (that *baiduMap) GetPosition(name string, region string) (string, error) {
|
2022-01-16 20:47:39 +00:00
|
|
|
|
|
|
|
client := &http.Client{}
|
2022-01-26 03:45:17 +00:00
|
|
|
if region == "" {
|
|
|
|
region = "全国"
|
|
|
|
}
|
2022-03-12 17:48:54 +00:00
|
|
|
reqest, err := http.NewRequest("GET", that.Url+"&query="+url.PathEscape(name)+"®ion="+url.PathEscape(region), nil)
|
2022-01-16 20:47:39 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Fatal error ", err.Error())
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
response, err := client.Do(reqest)
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Fatal error ", err.Error())
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
//fmt.Println(string(body))
|
|
|
|
|
|
|
|
return string(body), err
|
|
|
|
|
|
|
|
}
|