iedc-go/dri/baidu/map.go

59 lines
1.0 KiB
Go
Raw Normal View History

2022-01-16 20:47:39 +00:00
package baidu
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
type BaiduMap struct {
Ak string
Url string
}
func (this *BaiduMap) Init(Ak string) {
//"ak=ZeT902EZvVgIoGVWEFK3osUm"
this.Ak = Ak
2022-01-26 03:45:17 +00:00
this.Url = "https://api.map.baidu.com/place/v2/suggestion?output=json" + "&ak=" + Ak
2022-01-16 20:47:39 +00:00
//query
}
// GetPosition 获取定位列表
2022-01-26 03:45:17 +00:00
func (this *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 = "全国"
}
reqest, err := http.NewRequest("GET", this.Url+"&query="+url.PathEscape(name)+"&region="+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
}
var DefaultBaiDuMap BaiduMap
func init() {
DefaultBaiDuMap = BaiduMap{}
}