From cf7fc8a4bac1ac236ea08fa7c4535b006f579496 Mon Sep 17 00:00:00 2001 From: gazebo Date: Thu, 17 Oct 2019 13:51:56 +0800 Subject: [PATCH] +GeoCodeRegeo --- platformapi/autonavi/autonavi.go | 140 ++++++++++++++++++++++++++ platformapi/autonavi/autonavi_test.go | 19 ++++ 2 files changed, 159 insertions(+) diff --git a/platformapi/autonavi/autonavi.go b/platformapi/autonavi/autonavi.go index 9ff7e07c..40f5d7ad 100644 --- a/platformapi/autonavi/autonavi.go +++ b/platformapi/autonavi/autonavi.go @@ -70,6 +70,15 @@ const ( DistrictLevelStreet = 4 ) +const ( + RoadLevelAll = 0 // 显示所有道路 + RoadLevelMain = 1 // 过滤非主干道路,仅输出主干道路数据 + + HomeOrCorpDef = 0 // 不对召回的排序策略进行干扰。 + HomeOrCorpPreferHome = 1 // 综合大数据分析将居家相关的 POI 内容优先返回,即优化返回结果中 pois 字段的poi顺序。 + HomeOrCorpPreferCorp = 2 // 综合大数据分析将公司相关的 POI 内容优先返回,即优化返回结果中 pois 字段的poi顺序。 +) + var ( levelStr2IntMap = map[string]int{ "country": DistrictLevelCountry, @@ -103,6 +112,90 @@ type API struct { key string } +type BuildingOrNeighborInfo struct { + Name string `json:"name"` + Type string `json:"type"` +} + +type BusinessAreaInfo struct { + ID string `json:"id"` + Location string `json:"location"` + Name string `json:"name"` +} + +type StreetNumberInfo struct { + Direction string `json:"direction"` + Distance string `json:"distance"` + Location string `json:"location"` + Number string `json:"number"` + Street string `json:"street"` +} + +type AoiInfo struct { + Adcode string `json:"adcode"` + Area string `json:"area"` + Distance string `json:"distance"` + ID string `json:"id"` + Location string `json:"location"` + Name string `json:"name"` + Type string `json:"type"` +} + +type PoiInfo struct { + Address string `json:"address"` + Businessarea string `json:"businessarea"` + Direction string `json:"direction"` + Distance string `json:"distance"` + ID string `json:"id"` + Location string `json:"location"` + Name string `json:"name"` + Poiweight string `json:"poiweight"` + Tel string `json:"tel"` + Type string `json:"type"` +} + +type RoadinterInfo struct { + Direction string `json:"direction"` + Distance string `json:"distance"` + FirstID string `json:"first_id"` + FirstName string `json:"first_name"` + Location string `json:"location"` + SecondID string `json:"second_id"` + SecondName string `json:"second_name"` +} + +type RoadInfo struct { + Direction string `json:"direction"` + Distance string `json:"distance"` + ID string `json:"id"` + Location string `json:"location"` + Name string `json:"name"` +} + +type AddressComponentInfo struct { + Adcode string `json:"adcode"` + Building *BuildingOrNeighborInfo `json:"building"` + BusinessAreas []*BusinessAreaInfo `json:"businessAreas"` + City string `json:"city"` + Citycode string `json:"citycode"` + Country string `json:"country"` + District string `json:"district"` + Neighborhood *BuildingOrNeighborInfo `json:"neighborhood"` + Province string `json:"province"` + StreetNumber *StreetNumberInfo `json:"streetNumber"` + Towncode string `json:"towncode"` + Township string `json:"township"` +} + +type RegeoCodeInfo struct { + AddressComponent *AddressComponentInfo `json:"addressComponent"` + Aois []*AoiInfo `json:"aois"` + FormattedAddress string `json:"formatted_address"` + Pois []*PoiInfo `json:"pois"` + Roadinters []*RoadinterInfo `json:"roadinters"` + Roads []*RoadInfo `json:"roads"` +} + func New(key string, config ...*platformapi.APIConfig) *API { curConfig := platformapi.DefAPIConfig if len(config) > 0 { @@ -247,6 +340,53 @@ func (a *API) GetCoordinateFromAddress(address string, cityInfo string) (lng, la return lng, lat, 0 } +func (a *API) GeoCodeRegeo(coords []*Coordinate, radius int, isExt bool, poiTypes []string, roadLevel, homeOrCorp int) (coordInfoList []*RegeoCodeInfo, err error) { + coordStrList := make([]string, len(coords)) + for k, v := range coords { + coordStrList[k] = coordinate2String(v.Lng, v.Lat) + } + params := map[string]interface{}{ + "location": strings.Join(coordStrList, "|"), + "batch": len(coordStrList) > 1, + } + if radius > 0 { + params["radius"] = radius + } + if isExt { + params["extensions"] = "all" + params["roadlevel"] = roadLevel + if len(poiTypes) > 0 { + params["poitype"] = strings.Join(poiTypes, "|") + } + if homeOrCorp > 0 { + params["homeorcorp"] = homeOrCorp + } + } + result, err := a.AccessAPI("geocode/regeo", params) + if err == nil { + if len(coordStrList) > 1 { + err = utils.Map2StructByJson(result["regeocodes"], &coordInfoList, true) + } else { + coordInfoList = make([]*RegeoCodeInfo, 1) + err = utils.Map2StructByJson(result["regeocode"], &coordInfoList[0], true) + } + } + return coordInfoList, err +} + +func (a *API) GeoCodeRegeoSingle(lng, lat float64, radius int, isExt bool, poiTypes []string, roadLevel, homeOrCorp int) (coordInfo *RegeoCodeInfo, err error) { + coordInfoList, err := a.GeoCodeRegeo([]*Coordinate{ + &Coordinate{ + Lng: lng, + Lat: lat, + }, + }, radius, isExt, poiTypes, roadLevel, homeOrCorp) + if err == nil { + coordInfo = coordInfoList[0] + } + return coordInfo, err +} + // 这里的District指的是实际的District,有些市是没有区的,比如东莞,这种情况下返回的区码是一个假的区域,即市的编码加上9000000 func (a *API) GetCoordinateDistrictCode(lng, lat float64) (districtCode int) { result, err := a.GetCoordinateAreaInfo(lng, lat) diff --git a/platformapi/autonavi/autonavi_test.go b/platformapi/autonavi/autonavi_test.go index 26fd32a1..2ac13aae 100644 --- a/platformapi/autonavi/autonavi_test.go +++ b/platformapi/autonavi/autonavi_test.go @@ -93,3 +93,22 @@ func TestWalkingDistance(t *testing.T) { distance := autonaviAPI.WalkingDistance(104.057289, 30.694798, 104.066289, 30.695598) t.Logf("distance:%f", distance) } + +func TestGeoCodeRegeo(t *testing.T) { + lng := 104.052756 + lat := 30.685203 + result, err := autonaviAPI.GeoCodeRegeo([]*Coordinate{ + &Coordinate{ + Lng: lng, + Lat: lat, + }, + &Coordinate{ + Lng: lng, + Lat: lat, + }, + }, 0, false, nil, 0, 0) + if err != nil { + t.Fatal(err) + } + t.Log(utils.Format4Output(result, false)) +}