89 lines
2.6 KiB
Go
89 lines
2.6 KiB
Go
package utils
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
type ReverseGeocodingReq struct {
|
|
Ak string `json:"ak"`
|
|
Coordtype string `json:"coordtype"`
|
|
RetCoordtype string `json:"retCoordtype"`
|
|
Location string `json:"location"`
|
|
Output string `json:"output"`
|
|
Language string `json:"language"`
|
|
}
|
|
|
|
type ReverseGeocodingRes struct {
|
|
Status int `json:"status"`
|
|
Result struct {
|
|
Location struct {
|
|
Lng float64 `json:"lng"`
|
|
Lat float64 `json:"lat"`
|
|
} `json:"location"`
|
|
FormattedAddress string `json:"formatted_address"`
|
|
Edz struct {
|
|
Name string `json:"name"`
|
|
} `json:"edz"`
|
|
Business string `json:"business"`
|
|
AddressComponent struct {
|
|
Country string `json:"country"`
|
|
CountryCodeIso string `json:"country_code_iso"`
|
|
CountryCodeIso2 string `json:"country_code_iso2"`
|
|
CountryCode int `json:"country_code"`
|
|
Province string `json:"province"`
|
|
City string `json:"city"`
|
|
CityLevel int `json:"city_level"`
|
|
District string `json:"district"`
|
|
Town string `json:"town"`
|
|
TownCode string `json:"town_code"`
|
|
Distance string `json:"distance"`
|
|
Direction string `json:"direction"`
|
|
Adcode string `json:"adcode"`
|
|
Street string `json:"street"`
|
|
StreetNumber string `json:"street_number"`
|
|
} `json:"addressComponent"`
|
|
} `json:"result"`
|
|
}
|
|
|
|
// ReverseGeo 经纬度逆编码
|
|
func ReverseGeo(longitude, latitude string, language string) (address string, err error) {
|
|
var reverseGeocodingReq ReverseGeocodingReq
|
|
|
|
reverseGeocodingReq.Ak = "3bAjKGA0pv7qvszGe98RsVZ04Ob5r4ZZ"
|
|
reverseGeocodingReq.Coordtype = "gcj02ll"
|
|
reverseGeocodingReq.Output = "json"
|
|
reverseGeocodingReq.RetCoordtype = "gcj02ll"
|
|
reverseGeocodingReq.Location = strings.Join([]string{latitude, longitude}, ",")
|
|
reverseGeocodingReq.Language = language
|
|
|
|
url := "https://api.map.baidu.com/reverse_geocoding/v3/?ak=" + reverseGeocodingReq.Ak + "&output=" + reverseGeocodingReq.Output + "&coordtype=" + reverseGeocodingReq.Coordtype + "&location=" + reverseGeocodingReq.Location + "&ret_coordtype=" + reverseGeocodingReq.RetCoordtype + "&language=" + reverseGeocodingReq.Language
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
var results ReverseGeocodingRes
|
|
err = json.Unmarshal(body, &results)
|
|
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if results.Status != 0 {
|
|
address = "未知地址"
|
|
return address, err
|
|
}
|
|
|
|
address = results.Result.FormattedAddress
|
|
|
|
return address, nil
|
|
}
|