package gps_tool import ( "bytes" "encoding/json" "fmt" "io" "log" "net/http" "net/url" "strconv" "time" "github.com/pkg/errors" ) type AnNaQiGpsClient struct { AppCode string host string } func NewAnNaQiGpsClient(appCode string) *AnNaQiGpsClient { return &AnNaQiGpsClient{ AppCode: appCode, host: "https://jmgeocode.market.alicloudapi.com", } } func (n *AnNaQiGpsClient) GetGpsInfo(longitude, latitude float64) (res *AddressComponent, err error) { lo := strconv.FormatFloat(longitude, 'f', 6, 64) la := strconv.FormatFloat(latitude, 'f', 6, 64) location := lo + "," + la // 拼接URL var fullURL string fullURL, err = url.JoinPath(n.host, "geocode/regeo_query") if err != nil { return nil, errors.Wrapf(err, "获取gps:%s信息失败,拼接路径错误", location) } param := "location=" + location // 创建请求 var req *http.Request req, err = http.NewRequest(http.MethodPost, fullURL, bytes.NewBuffer([]byte(param))) if err != nil { return nil, errors.Wrapf(err, "获取gps:%s信息失败,创建请求失败", location) } // 设置请求头 req.Header.Add("Authorization", "APPCODE "+n.AppCode) req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") // 发送请求 // 创建HTTP客户端 client := &http.Client{} client.Timeout = 5 * time.Second var resp *http.Response resp, err = client.Do(req) if err != nil { return nil, errors.Wrapf(err, "获取gps:%s信息失败,发送请求失败", location) } if resp.StatusCode != http.StatusOK { return nil, errors.New(fmt.Sprintf("获取gps:%s信息失败,%+v", location, resp.Status)) } defer func(Body io.ReadCloser) { err = Body.Close() if err != nil { log.Printf("获取gps:%s信息,关闭响应体失败: %+v\n", location, err) } }(resp.Body) // 读取响应体 var body []byte body, err = io.ReadAll(resp.Body) if err != nil { return nil, errors.Wrapf(err, "获取gps:%s信息失败,读取响应体失败", location) } // 解析JSON响应 var apiResult ApiResult err = json.Unmarshal(body, &apiResult) if err != nil { return nil, errors.Wrapf(err, "获取gps:%s信息失败,解析JSON响应失败,%s", location, string(body)) } // 检查API返回状态 if apiResult.Code == 200 { if len(apiResult.Data.Regeocodes) > 0 { return &apiResult.Data.Regeocodes[0].AddressComponent, nil } } return nil, errors.New(fmt.Sprintf("获取gps:%s信息失败,%+v", location, apiResult)) } func (n *AnNaQiGpsClient) GetLocation(city, region, address string) (res []*Geocode, err error) { values := url.Values{} values.Set("city", city) values.Set("address", region+address) // 创建请求 var req *http.Request req, err = http.NewRequest(http.MethodPost, n.host+"/geocode/geo/query", bytes.NewBuffer([]byte(values.Encode()))) if err != nil { return nil, errors.Wrapf(err, "获取经纬度失败,创建请求失败") } // 设置请求头 req.Header.Add("Authorization", "APPCODE "+n.AppCode) req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") // 发送请求 // 创建HTTP客户端 client := &http.Client{} client.Timeout = 5 * time.Second var resp *http.Response resp, err = client.Do(req) if err != nil { return nil, errors.Wrapf(err, "获取经纬度失败,发送请求失败") } if resp.StatusCode != http.StatusOK { return nil, errors.New(fmt.Sprintf("获取经纬度失败,%+v", resp.Status)) } defer func(Body io.ReadCloser) { err = Body.Close() if err != nil { log.Printf("关闭获取经纬度响应体失败: %+v\n", err) } }(resp.Body) // 读取响应体 var body []byte body, err = io.ReadAll(resp.Body) if err != nil { return nil, errors.Wrapf(err, "获取经纬度失败,读取响应体失败") } // 解析JSON响应 var locationInfo LocationInfo err = json.Unmarshal(body, &locationInfo) if err != nil { return nil, errors.Wrapf(err, "获取经纬度失败,解析JSON响应失败,%s", string(body)) } // 检查API返回状态 if locationInfo.Code == 200 { if locationInfo.Data.Count > 0 { return locationInfo.Data.Geocodes, nil } } return nil, errors.New(fmt.Sprintf("获取经纬度失败,%+v", locationInfo.Msg)) }