瀏覽代碼

添加高德地图接口

huangrf 5 年之前
父節點
當前提交
f5490ae8c6
共有 2 個文件被更改,包括 261 次插入0 次删除
  1. 176 0
      third/amap/amap.go
  2. 85 0
      third/amap/global.go

+ 176 - 0
third/amap/amap.go

@@ -0,0 +1,176 @@
+package amap
+
+import (
+	"encoding/json"
+	"fmt"
+	"git.qianqiusoft.com/qianqiusoft/light-apiengine/utils"
+	"github.com/pkg/errors"
+)
+
+const(
+	__AMAP_API_ENDPOINT = "https://restapi.amap.com"
+	__KEY = "427befed1b2db94d6753b679d7330772"
+)
+
+type AMapClient struct{
+	Key string
+}
+
+/**
+ * @brief: 逆地理编码
+ * @param1 lngLats: 经纬度列表,{lng1,lat1,lng2,lat2}
+ * @return1: 逆转换结果
+ * @return2: 错误信息
+ */
+func (a *AMapClient)Regeo(lngLats []string)(*RegeoResult, error){
+	if lngLats == nil || len(lngLats) == 0{
+		return nil, errors.New("参数lnglats为空")
+	}
+	if len(lngLats) % 2 != 0{
+		return nil, errors.New("参数lnglats长度必须为2的倍数")
+	}
+	path := "/geocode/regeo"
+	location := ""
+	location = fmt.Sprintf("%s,%s", lngLats[0], lngLats[1])
+	for i := 2; i < len(lngLats); i += 2 {
+		location += "|" + fmt.Sprintf("%s,%s", lngLats[i], lngLats[i+1])
+	}
+
+	params := make(map[string]string)
+	params["location"] = location
+	params["batch"] = "true"
+
+	fullUrl := a.getFullUrl(path, params)
+	bytess, err := utils.NewHttpUtil().Get(fullUrl, nil, nil)
+	if err != nil{
+		return nil, err
+	}
+
+	regeoResult := &RegeoResult{}
+	err = json.Unmarshal(bytess, regeoResult)
+	if err != nil{
+		return nil, err
+	}
+
+	return regeoResult, nil
+}
+
+/**
+ * @brief: 坐标转换
+ * @param1 lngLats: 经纬度列表
+ * @param2 coordType: 坐标类型 gps;mapbar;baidu;autonavi
+ * @return1: 坐标转回结果
+ * @return2: 错误信息
+ */
+func (a *AMapClient)ConvCoord(lngLats []string, coordType string)(*ConvCoordResult, error){
+	locations := a.getLocations(lngLats)
+
+	params := make(map[string]string)
+	params["locations"] = locations
+	params["coordsys"] = coordType
+
+	path := "/assistant/coordinate/convert"
+
+	fullUrl := a.getFullUrl(path, params)
+	bytess, err := utils.NewHttpUtil().Get(fullUrl, nil, nil)
+	if err != nil{
+		return nil, err
+	}
+
+	result := &ConvCoordResult{}
+	err = json.Unmarshal(bytess, result)
+	if err != nil{
+		return nil, err
+	}
+
+	return result, nil
+}
+
+/**
+ * @brief: 获取天气
+ * @param1 city: 区编码
+ * @param2 forecast: 是否预报天汽水
+ * @return1: 天气结果
+ * @return2: 错误信息
+ */
+func (a *AMapClient)FetchWeather(city string, live bool)(*WeatherResult, error){
+	params := make(map[string]string)
+	params["city"] = city
+	if !live{
+		params["extensions"] = "all"
+	}
+
+	path := "/weather/weatherInfo"
+
+	fullUrl := a.getFullUrl(path, params)
+	bytess, err := utils.NewHttpUtil().Get(fullUrl, nil, nil)
+	if err != nil{
+		return nil, err
+	}
+
+	result := &WeatherResult{}
+	err = json.Unmarshal(bytess, result)
+	if err != nil{
+		return nil, err
+	}
+
+	return result, nil
+}
+
+/**
+ * @brief: 根据ip获取地址信息
+ * @param1 ip: ip,空标识使用请求ip获取地址
+ */
+func (a *AMapClient)FectionLocationByIp(ip string)(*IpResult, error){
+	if ip == ""{
+		return nil, errors.New("ip不能为空")
+	}
+	params := make(map[string]string)
+	params["ip"] = ip
+
+	path := "/ip"
+	fullUrl := a.getFullUrl(path, params)
+	bytess, err := utils.NewHttpUtil().Get(fullUrl, nil, nil)
+	if err != nil{
+		return nil, err
+	}
+
+	result := &IpResult{}
+	err = json.Unmarshal(bytess, result)
+	if err != nil{
+		return nil, err
+	}
+
+	return result, nil
+}
+
+/**
+ * @brief: 获取完全url
+ * @param1 path: 请求path
+ * @param2 params: 参数
+ */
+func (a *AMapClient)getFullUrl(path string, params map[string]string)string{
+	version := "v3"
+	paramsStr := ""
+	params["key"] = a.Key
+	for k, v := range params{
+		paramsStr += fmt.Sprintf("%s=%s&", k, v)
+	}
+
+	return __AMAP_API_ENDPOINT + "/" + version + path + "?" + paramsStr
+}
+
+/**
+ * @brief: 多个坐标转换程参数形式
+ * @param1 lngLats: 坐标列表
+ * @return1: 坐标结果 lng1,lat1|lng2,lat2
+ */
+func (a *AMapClient)getLocations(lngLats []string)string{
+	location := ""
+	location = fmt.Sprintf("%s,%s", lngLats[0], lngLats[1])
+	for i := 2; i < len(lngLats); i += 2 {
+		location += "|" + fmt.Sprintf("%s,%s", lngLats[i], lngLats[i+1])
+	}
+
+	return location
+}

+ 85 - 0
third/amap/global.go

@@ -0,0 +1,85 @@
+package amap
+
+/**
+ * 逆地理编码结果
+ */
+type RegeoResult struct {
+	Status    string   `json:"status"`
+	Info      string   `json:"info"`
+	InfoCode  string   `json:"infocode"`
+	RegeoCodes []*RegoCode `json:"regeocodes"`
+}
+
+/**
+ * 逆地理编码地理信息,暂时只解析出地址
+ */
+type RegoCode struct {
+	FormattedAddress string `json:"formatted_address"`
+}
+
+/**
+ * 坐标转换的结果
+ */
+type ConvCoordResult struct {
+	Status    string `json:"status"`
+	Info      string `json:"info"`
+	InfoCode  string `json:"infocode"`
+	Locations string `json:"locations"`
+}
+
+/**
+ * 天气结果
+ */
+type WeatherResult struct {
+	Status   string    `json:"status"`
+	Info     string    `json:"info"`
+	InfoCode string    `json:"infocode"`
+	Lives    []*Live   `json:"lives"`	// 实况天气
+	Forecasts []*Forecast `json:"forecasts"` // 预报天气
+}
+
+type Live struct {
+	Province      string `json:"province"`
+	City          string `json:"city"`
+	AdCode        string `json:"adcode"`
+	Weather       string `json:"weather"`
+	Termperature  string `json:"temperature"`
+	WindDirection string `json:"winddirection"`
+	WindPower     string `json:"windpower"`
+	Humidity      string `json:"humidity"`
+	ReportTime    string `json:"reporttime"`
+}
+
+type Forecast struct{
+	Province      string `json:"province"`
+	City          string `json:"city"`
+	AdCode        string `json:"adcode"`
+	ReportTime    string `json:"reporttime"`
+	Casts []*Cast `json:"casts"`
+}
+
+type Cast struct {
+	Date         string `json:"date"`
+	Week         string `json:"week"`
+	DayWeather   string `json:"dayweather"`
+	NightWeather string `json:"nightweather"`
+	DayTemp      string `json:"daytemp"`
+	NightTemp    string `json:"nighttemp"`
+	DayWind      string `json:"daywind"`
+	NightWind    string `json:"nightwind"`
+	DayPower     string `json:"daypower"`
+	NightPower   string `json:"nightpower"`
+}
+
+/**
+ * 根据ip获取位置结果
+ */
+type IpResult struct {
+	Status    string   `json:"status"`
+	Info      string   `json:"info"`
+	InfoCode  string   `json:"infocode"`
+	Province  string `json:"province"`
+	City      string   `json:"city"`
+	AdCode    string   `json:"adcode"`
+	Rectangle string   `json:"rectangle"`
+}