2024-06-28 22:07:15 +08:00
|
|
|
package baidu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/smbrave/goutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type addressRsp struct {
|
|
|
|
Status int `json:"status"`
|
|
|
|
Result struct {
|
|
|
|
FormattedAddress string `json:"formatted_address"`
|
|
|
|
} `json:"result"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Lbs struct {
|
|
|
|
ak string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLbs(ak string) *Lbs {
|
|
|
|
return &Lbs{
|
|
|
|
ak: ak,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Lbs) GetAddress(lng, lat float64) (string, error) {
|
2024-06-28 22:24:42 +08:00
|
|
|
reqUrl := fmt.Sprintf("https://api.map.baidu.com/reverse_geocoding/v3/?ak=%s&output=json&coordtype=wgs84&location=%f,%f", l.ak, lat, lng)
|
2024-06-28 22:07:15 +08:00
|
|
|
rspBody, err := goutil.HttpGet(reqUrl, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
var rsp addressRsp
|
|
|
|
if err := json.Unmarshal(rspBody, &rsp); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if rsp.Status != 0 {
|
|
|
|
return "", errors.New(string(rspBody))
|
|
|
|
}
|
|
|
|
return rsp.Result.FormattedAddress, nil
|
|
|
|
}
|