gosdk/baidu/lbs.go

42 lines
813 B
Go

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) {
reqUrl := fmt.Sprintf("https://api.map.baidu.com/reverse_geocoding/v3/?ak=%s&output=json&coordtype=wgs84&location=%f,%f", l.ak, lat, lng)
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
}