amap.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package amap
  2. import (
  3. "cold-logistics/conf"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/go-resty/resty/v2"
  7. "strconv"
  8. )
  9. type Address struct {
  10. Status string `json:"status"`
  11. Regeocode Regeocode `json:"regeocode"`
  12. Info string `json:"info"`
  13. Infocode string `json:"infocode"`
  14. }
  15. type Regeocode struct {
  16. AddressComponent AddressComponent `json:"addressComponent"`
  17. FormattedAddress string `json:"formatted_address"`
  18. }
  19. type AddressComponent struct {
  20. City string `json:"city"`
  21. Province string `json:"province"`
  22. Adcode string `json:"adcode"`
  23. District string `json:"district"`
  24. Township string `json:"township"`
  25. StreetNumber StreetNumber `json:"streetNumber"`
  26. Country string `json:"country"`
  27. Citycode string `json:"citycode"`
  28. }
  29. type StreetNumber struct {
  30. Number string `json:"number"`
  31. Location string `json:"location"`
  32. Direction string `json:"direction"`
  33. Distance string `json:"distance"`
  34. Street string `json:"street"`
  35. }
  36. // 高德-逆地址解析
  37. func GeocodeRegeo(lng, lat string) (address Address) {
  38. if len(lng) == 0 || len(lat) == 0 {
  39. return
  40. }
  41. lngFloat64, err := strconv.ParseFloat(lng, 64)
  42. if err != nil {
  43. return
  44. }
  45. lngStr := fmt.Sprintf("%.6f", lngFloat64)
  46. latFloat64, err := strconv.ParseFloat(lat, 64)
  47. if err != nil {
  48. return
  49. }
  50. latStr := fmt.Sprintf("%.6f", latFloat64)
  51. client := resty.New()
  52. resp, err := client.R().SetQueryParams(map[string]string{
  53. "key": conf.ExtConfig.Amap.Key,
  54. "location": lngStr + "," + latStr,
  55. }).Get("https://restapi.amap.com/v3/geocode/regeo")
  56. if err != nil {
  57. return
  58. }
  59. err = json.Unmarshal(resp.Body(), &address)
  60. if err != nil {
  61. return
  62. }
  63. return address
  64. }