增加地址解析

This commit is contained in:
yuguojian
2025-07-17 17:16:49 +08:00
parent aee7202752
commit d0d264d1e7
3 changed files with 189 additions and 73 deletions

View File

@@ -16,13 +16,13 @@ import (
type AnNaQiGpsClient struct {
AppCode string
Host string
host string
}
func NewAnNaQiGpsClient(appCode string) *AnNaQiGpsClient {
return &AnNaQiGpsClient{
AppCode: appCode,
Host: "https://jmgeocode.market.alicloudapi.com",
host: "https://jmgeocode.market.alicloudapi.com",
}
}
@@ -32,7 +32,7 @@ func (n *AnNaQiGpsClient) GetGpsInfo(longitude, latitude float64) (res *AddressC
location := lo + "," + la
// 拼接URL
var fullURL string
fullURL, err = url.JoinPath(n.Host, "geocode/regeo_query")
fullURL, err = url.JoinPath(n.host, "geocode/regeo_query")
if err != nil {
return nil, errors.Wrapf(err, "获取gps:%s信息失败拼接路径错误", location)
}
@@ -56,6 +56,9 @@ func (n *AnNaQiGpsClient) GetGpsInfo(longitude, latitude float64) (res *AddressC
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 {
@@ -76,67 +79,67 @@ func (n *AnNaQiGpsClient) GetGpsInfo(longitude, latitude float64) (res *AddressC
return nil, errors.Wrapf(err, "获取gps:%s信息失败解析JSON响应失败%s", location, string(body))
}
// 检查API返回状态
if apiResult.Code != 200 {
return nil, errors.New(fmt.Sprintf("获取gps:%s信息失败%+v", location, apiResult))
if apiResult.Code == 200 {
if len(apiResult.Data.Regeocodes) > 0 {
return &apiResult.Data.Regeocodes[0].AddressComponent, nil
}
}
if len(apiResult.Data.Regeocodes) > 0 {
return &apiResult.Data.Regeocodes[0].AddressComponent, nil
}
return nil, errors.New(fmt.Sprintf("获取gps:%s信息失败%+v", location, apiResult))
}
type ApiResult struct {
Data Data `json:"data"`
Msg string `json:"msg"`
Success bool `json:"success"`
Code int `json:"code"`
TaskNo string `json:"taskNo"`
}
func (n *AnNaQiGpsClient) GetLocation(city, region, address string) (res *[]Geocode, err error) {
values := url.Values{}
values.Set("city", city)
values.Set("address", region+address)
type Data struct {
Regeocodes []Regeocode `json:"regeocodes"`
}
// 创建请求
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")
type Regeocode struct {
FormattedAddress string `json:"formatted_address"`
AddressComponent AddressComponent `json:"addressComponent"`
}
// 发送请求
// 创建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, "获取经纬度失败,读取响应体失败")
}
type AddressComponent struct {
// BusinessAreas []interface{} `json:"businessAreas"`
Country string `json:"country"`
Province string `json:"province"`
Citycode string `json:"citycode"`
City string `json:"city"`
Adcode string `json:"adcode"`
// StreetNumber StreetNumber `json:"streetNumber"`
// Towncode string `json:"towncode"`
// District string `json:"district"`
// Neighborhood Neighborhood `json:"neighborhood"`
// Township string `json:"township"`
// Building Building `json:"building"`
}
// 解析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 len(locationInfo.Data.Geocodes) > 0 {
return &locationInfo.Data.Geocodes, nil
}
}
type BusinessArea struct {
Name string `json:"name"`
Location string `json:"location"`
Id string `json:"id"`
}
type StreetNumber struct {
Number string `json:"number"`
Distance string `json:"distance"`
Street string `json:"street"`
Location string `json:"location"`
Direction string `json:"direction"`
}
type Neighborhood struct {
Name interface{} `json:"name"`
Type interface{} `json:"type"`
}
type Building struct {
Name interface{} `json:"name"`
Type interface{} `json:"type"`
return nil, errors.New(fmt.Sprintf("获取经纬度失败,%+v", locationInfo.Msg))
}