From 2bb6fd0d2e192149ec80cd833da3ade832831393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Tue, 4 Apr 2023 17:10:22 +0800 Subject: [PATCH] - update --- library.go | 2 +- service/amap/geocode.geo.go | 66 ++++++++++++ service/amap/geocode.regeo.go | 113 +++++++++++++++++++ service/baidu/geocoding.go | 51 +++++++++ service/baidu/reverse_geocoding.go | 71 ++++++++++++ service/caiyunapp/daily.go | 2 +- service/caiyunapp/hourly.go | 2 +- service/caiyunapp/minutely.go | 2 +- service/caiyunapp/realtime.go | 2 +- service/caiyunapp/weather.go | 2 +- service/qq/geocoder.address.go | 63 +++++++++++ service/qq/geocoder.location.go | 168 +++++++++++++++++++++++++++++ 12 files changed, 538 insertions(+), 6 deletions(-) create mode 100644 service/amap/geocode.geo.go create mode 100644 service/amap/geocode.regeo.go create mode 100644 service/baidu/geocoding.go create mode 100644 service/baidu/reverse_geocoding.go create mode 100644 service/qq/geocoder.address.go create mode 100644 service/qq/geocoder.location.go diff --git a/library.go b/library.go index b987c747..cee30c1c 100644 --- a/library.go +++ b/library.go @@ -1,5 +1,5 @@ package go_library func Version() string { - return "1.0.129" + return "1.0.130" } diff --git a/service/amap/geocode.geo.go b/service/amap/geocode.geo.go new file mode 100644 index 00000000..76177aeb --- /dev/null +++ b/service/amap/geocode.geo.go @@ -0,0 +1,66 @@ +package amap + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" + "net/http" +) + +type GeocodeGeoResponse struct { + Status string `json:"status"` + Info string `json:"info"` + Infocode string `json:"infocode"` + Count string `json:"count"` + Geocodes []struct { + FormattedAddress string `json:"formatted_address"` + Country string `json:"country"` + Province string `json:"province"` + Citycode string `json:"citycode"` + City string `json:"city"` + District string `json:"district"` + Township []interface{} `json:"township"` + Neighborhood struct { + Name []interface{} `json:"name"` + Type []interface{} `json:"type"` + } `json:"neighborhood"` + Building struct { + Name []interface{} `json:"name"` + Type []interface{} `json:"type"` + } `json:"building"` + Adcode string `json:"adcode"` + Street string `json:"street"` + Number string `json:"number"` + Location string `json:"location"` + Level string `json:"level"` + } `json:"geocodes"` +} + +type GeocodeGeoResult struct { + Result GeocodeGeoResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newGeocodeGeoResult(result GeocodeGeoResponse, body []byte, http gorequest.Response) *GeocodeGeoResult { + return &GeocodeGeoResult{Result: result, Body: body, Http: http} +} + +// GeocodeGeo 地理编码 +// https://lbs.amap.com/api/webservice/guide/api/georegeo +func (c *Client) GeocodeGeo(ctx context.Context, address string, notMustParams ...gorequest.Params) (*GeocodeGeoResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + params.Set("key", c.GetKey()) + params.Set("address", address) + params.Set("output", "JSON") + // 请求 + request, err := c.request(ctx, apiUrl+"/geocode/geo", params, http.MethodGet) + if err != nil { + return newGeocodeGeoResult(GeocodeGeoResponse{}, request.ResponseBody, request), err + } + // 定义 + var response GeocodeGeoResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newGeocodeGeoResult(response, request.ResponseBody, request), err +} diff --git a/service/amap/geocode.regeo.go b/service/amap/geocode.regeo.go new file mode 100644 index 00000000..15bf1f9e --- /dev/null +++ b/service/amap/geocode.regeo.go @@ -0,0 +1,113 @@ +package amap + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" + "net/http" +) + +type GeocodeRegeoResponse struct { + Status string `json:"status"` + Regeocode struct { + Roads []struct { + Id string `json:"id"` + Location string `json:"location"` + Direction string `json:"direction"` + Name string `json:"name"` + Distance string `json:"distance"` + } `json:"roads"` + Roadinters []struct { + SecondName string `json:"second_name"` + FirstId string `json:"first_id"` + SecondId string `json:"second_id"` + Location string `json:"location"` + Distance string `json:"distance"` + FirstName string `json:"first_name"` + Direction string `json:"direction"` + } `json:"roadinters"` + FormattedAddress string `json:"formatted_address"` + AddressComponent struct { + City []interface{} `json:"city"` + Province string `json:"province"` + Adcode string `json:"adcode"` + District string `json:"district"` + Towncode string `json:"towncode"` + StreetNumber struct { + Number string `json:"number"` + Location string `json:"location"` + Direction string `json:"direction"` + Distance string `json:"distance"` + Street string `json:"street"` + } `json:"streetNumber"` + Country string `json:"country"` + Township string `json:"township"` + BusinessAreas []struct { + Location string `json:"location"` + Name string `json:"name"` + Id string `json:"id"` + } `json:"businessAreas"` + Building struct { + Name string `json:"name"` + Type string `json:"type"` + } `json:"building"` + Neighborhood struct { + Name string `json:"name"` + Type string `json:"type"` + } `json:"neighborhood"` + Citycode string `json:"citycode"` + } `json:"addressComponent"` + Aois []struct { + Area string `json:"area"` + Type string `json:"type"` + Id string `json:"id"` + Location string `json:"location"` + Adcode string `json:"adcode"` + Name string `json:"name"` + Distance string `json:"distance"` + } `json:"aois"` + Pois []struct { + Id string `json:"id"` + Direction string `json:"direction"` + Businessarea string `json:"businessarea"` + Address string `json:"address"` + Poiweight string `json:"poiweight"` + Name string `json:"name"` + Location string `json:"location"` + Distance string `json:"distance"` + Tel interface{} `json:"tel"` + Type string `json:"type"` + } `json:"pois"` + } `json:"regeocode"` + Info string `json:"info"` + Infocode string `json:"infocode"` +} + +type GeocodeRegeoResult struct { + Result GeocodeRegeoResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newGeocodeRegeoResult(result GeocodeRegeoResponse, body []byte, http gorequest.Response) *GeocodeRegeoResult { + return &GeocodeRegeoResult{Result: result, Body: body, Http: http} +} + +// GeocodeRegeo 逆地理编码 +// https://lbs.amap.com/api/webservice/guide/api/georegeo +func (c *Client) GeocodeRegeo(ctx context.Context, location string, notMustParams ...gorequest.Params) (*GeocodeRegeoResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + params.Set("key", c.GetKey()) + params.Set("location", location) + params.Set("output", "JSON") + // 请求 + request, err := c.request(ctx, apiUrl+"/geocode/regeo", params, http.MethodGet) + if err != nil { + return newGeocodeRegeoResult(GeocodeRegeoResponse{}, request.ResponseBody, request), err + } + // 定义 + var response GeocodeRegeoResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newGeocodeRegeoResult(response, request.ResponseBody, request), err +} diff --git a/service/baidu/geocoding.go b/service/baidu/geocoding.go new file mode 100644 index 00000000..5f08e0d8 --- /dev/null +++ b/service/baidu/geocoding.go @@ -0,0 +1,51 @@ +package baidu + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" + "net/http" +) + +type GeocodingResponse struct { + Status int `json:"status"` + Result struct { + Location struct { + Lng float64 `json:"lng"` + Lat float64 `json:"lat"` + } `json:"location"` + Precise int `json:"precise"` + Confidence int `json:"confidence"` + Comprehension int `json:"comprehension"` + Level string `json:"level"` + } `json:"result"` +} + +type GeocodingResult struct { + Result GeocodingResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newGeocodingResult(result GeocodingResponse, body []byte, http gorequest.Response) *GeocodingResult { + return &GeocodingResult{Result: result, Body: body, Http: http} +} + +// Geocoding 地理编码服务 +// https://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding +func (c *Client) Geocoding(ctx context.Context, address string, notMustParams ...gorequest.Params) (*GeocodingResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + params.Set("ak", c.GetAk()) + params.Set("address", address) + params.Set("output", "json") + // 请求 + request, err := c.request(ctx, apiUrl+"/geocoding/v3/", params, http.MethodGet) + if err != nil { + return newGeocodingResult(GeocodingResponse{}, request.ResponseBody, request), err + } + // 定义 + var response GeocodingResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newGeocodingResult(response, request.ResponseBody, request), err +} diff --git a/service/baidu/reverse_geocoding.go b/service/baidu/reverse_geocoding.go new file mode 100644 index 00000000..982c9072 --- /dev/null +++ b/service/baidu/reverse_geocoding.go @@ -0,0 +1,71 @@ +package baidu + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" + "net/http" +) + +type ReverseGeocodingResponse struct { + Status int `json:"status"` + Result struct { + Location struct { + Lng float64 `json:"lng"` + Lat float64 `json:"lat"` + } `json:"location"` + FormattedAddress string `json:"formatted_address"` + Business string `json:"business"` + AddressComponent struct { + Country string `json:"country"` + CountryCode int `json:"country_code"` + CountryCodeIso string `json:"country_code_iso"` + CountryCodeIso2 string `json:"country_code_iso2"` + 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"` + Pois []interface{} `json:"pois"` + Roads []interface{} `json:"roads"` + PoiRegions []interface{} `json:"poiRegions"` + SematicDescription string `json:"sematic_description"` + CityCode int `json:"cityCode"` + } `json:"result"` +} + +type ReverseGeocodingResult struct { + Result ReverseGeocodingResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newReverseGeocodingResult(result ReverseGeocodingResponse, body []byte, http gorequest.Response) *ReverseGeocodingResult { + return &ReverseGeocodingResult{Result: result, Body: body, Http: http} +} + +// ReverseGeocoding 全球逆地理编码服务 +// https://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding-abroad +func (c *Client) ReverseGeocoding(ctx context.Context, location string, notMustParams ...gorequest.Params) (*ReverseGeocodingResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + params.Set("ak", c.GetAk()) + params.Set("location", location) + params.Set("output", "json") + // 请求 + request, err := c.request(ctx, apiUrl+"/reverse_geocoding/v3/", params, http.MethodGet) + if err != nil { + return newReverseGeocodingResult(ReverseGeocodingResponse{}, request.ResponseBody, request), err + } + // 定义 + var response ReverseGeocodingResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newReverseGeocodingResult(response, request.ResponseBody, request), err +} diff --git a/service/caiyunapp/daily.go b/service/caiyunapp/daily.go index 04d90776..21771a0d 100644 --- a/service/caiyunapp/daily.go +++ b/service/caiyunapp/daily.go @@ -223,7 +223,7 @@ func newDailyResult(result DailyResponse, body []byte, http gorequest.Response) // Daily 天级别预报 // https://docs.caiyunapp.com/docs/daily -func (c *Client) Daily(ctx context.Context, locationLatitude, locationLongitude string, notMustParams ...gorequest.Params) (*DailyResult, error) { +func (c *Client) Daily(ctx context.Context, locationLongitude, locationLatitude string, notMustParams ...gorequest.Params) (*DailyResult, error) { // 参数 params := gorequest.NewParamsWith(notMustParams...) // 请求 diff --git a/service/caiyunapp/hourly.go b/service/caiyunapp/hourly.go index 0e749a05..0f84d65d 100644 --- a/service/caiyunapp/hourly.go +++ b/service/caiyunapp/hourly.go @@ -95,7 +95,7 @@ func newHourlyResult(result HourlyResponse, body []byte, http gorequest.Response // Hourly 小时级别预报 // https://docs.caiyunapp.com/docs/hourly -func (c *Client) Hourly(ctx context.Context, locationLatitude, locationLongitude string, notMustParams ...gorequest.Params) (*HourlyResult, error) { +func (c *Client) Hourly(ctx context.Context, locationLongitude, locationLatitude string, notMustParams ...gorequest.Params) (*HourlyResult, error) { // 参数 params := gorequest.NewParamsWith(notMustParams...) // 请求 diff --git a/service/caiyunapp/minutely.go b/service/caiyunapp/minutely.go index f21141fc..939df2ef 100644 --- a/service/caiyunapp/minutely.go +++ b/service/caiyunapp/minutely.go @@ -44,7 +44,7 @@ func newMinutelyResult(result MinutelyResponse, body []byte, http gorequest.Resp // Minutely 分钟级预报 // https://docs.caiyunapp.com/docs/minutely -func (c *Client) Minutely(ctx context.Context, locationLatitude, locationLongitude string, notMustParams ...gorequest.Params) (*MinutelyResult, error) { +func (c *Client) Minutely(ctx context.Context, locationLongitude, locationLatitude string, notMustParams ...gorequest.Params) (*MinutelyResult, error) { // 参数 params := gorequest.NewParamsWith(notMustParams...) // 请求 diff --git a/service/caiyunapp/realtime.go b/service/caiyunapp/realtime.go index 56df9c8c..3fd37b38 100644 --- a/service/caiyunapp/realtime.go +++ b/service/caiyunapp/realtime.go @@ -88,7 +88,7 @@ func newRealtimeResult(result RealtimeResponse, body []byte, http gorequest.Resp // Realtime 实况 // https://docs.caiyunapp.com/docs/realtime -func (c *Client) Realtime(ctx context.Context, locationLatitude, locationLongitude string, notMustParams ...gorequest.Params) (*RealtimeResult, error) { +func (c *Client) Realtime(ctx context.Context, locationLongitude, locationLatitude string, notMustParams ...gorequest.Params) (*RealtimeResult, error) { // 参数 params := gorequest.NewParamsWith(notMustParams...) // 请求 diff --git a/service/caiyunapp/weather.go b/service/caiyunapp/weather.go index ec81f1bb..31ed1388 100644 --- a/service/caiyunapp/weather.go +++ b/service/caiyunapp/weather.go @@ -46,7 +46,7 @@ func newWeatherResult(result WeatherResponse, body []byte, http gorequest.Respon // Weather 综合 // https://docs.caiyunapp.com/docs/weather -func (c *Client) Weather(ctx context.Context, locationLatitude, locationLongitude string, notMustParams ...gorequest.Params) (*WeatherResult, error) { +func (c *Client) Weather(ctx context.Context, locationLongitude, locationLatitude string, notMustParams ...gorequest.Params) (*WeatherResult, error) { // 参数 params := gorequest.NewParamsWith(notMustParams...) // 请求 diff --git a/service/qq/geocoder.address.go b/service/qq/geocoder.address.go new file mode 100644 index 00000000..1edc1d66 --- /dev/null +++ b/service/qq/geocoder.address.go @@ -0,0 +1,63 @@ +package qq + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" + "net/http" +) + +type GeocoderAddressResponse struct { + Status int `json:"status"` + Message string `json:"message"` + Result struct { + Title string `json:"title"` + Location struct { + Lng float64 `json:"lng"` + Lat float64 `json:"lat"` + } `json:"location"` + AdInfo struct { + Adcode string `json:"adcode"` + } `json:"ad_info"` + AddressComponents struct { + Province string `json:"province"` + City string `json:"city"` + District string `json:"district"` + Street string `json:"street"` + StreetNumber string `json:"street_number"` + } `json:"address_components"` + Similarity float64 `json:"similarity"` + Deviation int `json:"deviation"` + Reliability int `json:"reliability"` + Level int `json:"level"` + } `json:"result"` +} + +type GeocoderAddressResult struct { + Result GeocoderAddressResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newGeocoderAddressResult(result GeocoderAddressResponse, body []byte, http gorequest.Response) *GeocoderAddressResult { + return &GeocoderAddressResult{Result: result, Body: body, Http: http} +} + +// GeocoderAddress 地址解析(地址转坐标) +// https://lbs.qq.com/service/webService/webServiceGuide/webServiceGeocoder +func (c *Client) GeocoderAddress(ctx context.Context, address string, notMustParams ...gorequest.Params) (*GeocoderAddressResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + params.Set("key", c.GetKey()) + params.Set("address", address) + params.Set("output", "JSON") + // 请求 + request, err := c.request(ctx, apiUrl+"/ws/geocoder/v1/", params, http.MethodGet) + if err != nil { + return newGeocoderAddressResult(GeocoderAddressResponse{}, request.ResponseBody, request), err + } + // 定义 + var response GeocoderAddressResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newGeocoderAddressResult(response, request.ResponseBody, request), err +} diff --git a/service/qq/geocoder.location.go b/service/qq/geocoder.location.go new file mode 100644 index 00000000..236850fc --- /dev/null +++ b/service/qq/geocoder.location.go @@ -0,0 +1,168 @@ +package qq + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" + "net/http" +) + +type GeocoderLocationResponse struct { + Status int `json:"status"` + Message string `json:"message"` + RequestId string `json:"request_id"` + Result struct { + Location struct { + Lat float64 `json:"lat"` + Lng float64 `json:"lng"` + } `json:"location"` + Address string `json:"address"` + FormattedAddresses struct { + Recommend string `json:"recommend"` + Rough string `json:"rough"` + } `json:"formatted_addresses"` + AddressComponent struct { + Nation string `json:"nation"` + Province string `json:"province"` + City string `json:"city"` + District string `json:"district"` + Street string `json:"street"` + StreetNumber string `json:"street_number"` + } `json:"address_component"` + AdInfo struct { + NationCode string `json:"nation_code"` + Adcode string `json:"adcode"` + PhoneAreaCode string `json:"phone_area_code"` + CityCode string `json:"city_code"` + Name string `json:"name"` + Location struct { + Lat float64 `json:"lat"` + Lng float64 `json:"lng"` + } `json:"location"` + Nation string `json:"nation"` + Province string `json:"province"` + City string `json:"city"` + District string `json:"district"` + } `json:"ad_info"` + AddressReference struct { + BusinessArea struct { + Id string `json:"id"` + Title string `json:"title"` + Location struct { + Lat float64 `json:"lat"` + Lng float64 `json:"lng"` + } `json:"location"` + Distance int `json:"_distance"` + DirDesc string `json:"_dir_desc"` + } `json:"business_area"` + FamousArea struct { + Id string `json:"id"` + Title string `json:"title"` + Location struct { + Lat float64 `json:"lat"` + Lng float64 `json:"lng"` + } `json:"location"` + Distance int `json:"_distance"` + DirDesc string `json:"_dir_desc"` + } `json:"famous_area"` + Crossroad struct { + Id string `json:"id"` + Title string `json:"title"` + Location struct { + Lat float64 `json:"lat"` + Lng float64 `json:"lng"` + } `json:"location"` + Distance float64 `json:"_distance"` + DirDesc string `json:"_dir_desc"` + } `json:"crossroad"` + Town struct { + Id string `json:"id"` + Title string `json:"title"` + Location struct { + Lat float64 `json:"lat"` + Lng float64 `json:"lng"` + } `json:"location"` + Distance int `json:"_distance"` + DirDesc string `json:"_dir_desc"` + } `json:"town"` + StreetNumber struct { + Id string `json:"id"` + Title string `json:"title"` + Location struct { + Lat float64 `json:"lat"` + Lng float64 `json:"lng"` + } `json:"location"` + Distance float64 `json:"_distance"` + DirDesc string `json:"_dir_desc"` + } `json:"street_number"` + Street struct { + Id string `json:"id"` + Title string `json:"title"` + Location struct { + Lat float64 `json:"lat"` + Lng float64 `json:"lng"` + } `json:"location"` + Distance float64 `json:"_distance"` + DirDesc string `json:"_dir_desc"` + } `json:"street"` + LandmarkL2 struct { + Id string `json:"id"` + Title string `json:"title"` + Location struct { + Lat float64 `json:"lat"` + Lng float64 `json:"lng"` + } `json:"location"` + Distance int `json:"_distance"` + DirDesc string `json:"_dir_desc"` + } `json:"landmark_l2"` + } `json:"address_reference"` + PoiCount int `json:"poi_count"` + Pois []struct { + Id string `json:"id"` + Title string `json:"title"` + Address string `json:"address"` + Category string `json:"category"` + Location struct { + Lat float64 `json:"lat"` + Lng float64 `json:"lng"` + } `json:"location"` + AdInfo struct { + Adcode string `json:"adcode"` + Province string `json:"province"` + City string `json:"city"` + District string `json:"district"` + } `json:"ad_info"` + Distance float64 `json:"_distance"` + DirDesc string `json:"_dir_desc,omitempty"` + } `json:"pois"` + } `json:"result"` +} + +type GeocoderLocationResult struct { + Result GeocoderLocationResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newGeocoderLocationResult(result GeocoderLocationResponse, body []byte, http gorequest.Response) *GeocoderLocationResult { + return &GeocoderLocationResult{Result: result, Body: body, Http: http} +} + +// GeocoderLocation 逆地址解析(坐标位置描述) +// https://lbs.qq.com/service/webService/webServiceGuide/webServiceGcoder +func (c *Client) GeocoderLocation(ctx context.Context, location string, notMustParams ...gorequest.Params) (*GeocoderLocationResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + params.Set("key", c.GetKey()) + params.Set("location", location) + params.Set("output", "JSON") + // 请求 + request, err := c.request(ctx, apiUrl+"/ws/geocoder/v1/", params, http.MethodGet) + if err != nil { + return newGeocoderLocationResult(GeocoderLocationResponse{}, request.ResponseBody, request), err + } + // 定义 + var response GeocoderLocationResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newGeocoderLocationResult(response, request.ResponseBody, request), err +}