+autonavi.BatchWalkingDistance

This commit is contained in:
gazebo
2019-10-18 12:02:14 +08:00
parent ef90a430ac
commit 629aa20fea
3 changed files with 162 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
package autonavi
import (
"bytes"
"crypto/md5"
"fmt"
"net/http"
@@ -12,8 +13,12 @@ import (
)
const (
signKey = "sig"
prodURL = "https://restapi.amap.com/v3"
signKey = "sig"
prodURL = "https://restapi.amap.com"
prodURLv3 = "/v3"
prodURLFullv3 = prodURL + prodURLv3
batchAPIStr = "batch"
)
const (
@@ -196,6 +201,16 @@ type RegeoCodeInfo struct {
Roads []*RoadInfo `json:"roads"`
}
type tBatchAPIParams struct {
APIStr string
APIParams map[string]interface{}
}
type tBatchAPIResponse struct {
Result ResponseResult
Err error
}
func New(key string, config ...*platformapi.APIConfig) *API {
curConfig := platformapi.DefAPIConfig
if len(config) > 0 {
@@ -236,7 +251,7 @@ func (a *API) AccessAPI(apiStr string, params map[string]interface{}) (retVal Re
err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request {
request, _ := http.NewRequest(http.MethodGet, utils.GenerateGetURL(prodURL, apiStr, params2), nil)
request, _ := http.NewRequest(http.MethodGet, utils.GenerateGetURL(prodURLFullv3, apiStr, params2), nil)
return request
},
a.config,
@@ -262,6 +277,74 @@ func (a *API) AccessAPI(apiStr string, params map[string]interface{}) (retVal Re
return retVal, err
}
func (a *API) BatchAccessAPI(apiList []*tBatchAPIParams) (retVal []*tBatchAPIResponse, err error) {
if len(apiList) == 0 {
return nil, nil
}
var ops []map[string]interface{}
for _, v := range apiList {
params2 := utils.MergeMaps(utils.Params2Map("key", a.key, "output", "json"), v.APIParams)
params2[signKey] = a.signParams(params2)
op := make(map[string]interface{})
op["url"] = utils.GenerateGetURL(prodURLv3, v.APIStr, params2)
ops = append(ops, op)
}
params2 := map[string]interface{}{
"ops": ops,
}
err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request {
request, _ := http.NewRequest(http.MethodGet, utils.GenerateGetURL(prodURLFullv3, batchAPIStr, utils.Params2Map("key", a.key)),
bytes.NewReader(utils.MustMarshal(params2)))
request.Header.Set("charset", "UTF-8")
request.Header.Set("Content-Type", "application/json")
return request
},
a.config,
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
if jsonResult1 == nil {
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
}
if resultList, ok := jsonResult1[platformapi.KeyData].([]interface{}); ok {
for _, v := range resultList {
jsonResult1 := v.(map[string]interface{})
status := int(utils.ForceInterface2Int64(jsonResult1["status"]))
if status == http.StatusOK {
jsonResult1 := v.(map[string]interface{})["body"].(map[string]interface{})
var retVal2 map[string]interface{}
var err2 error
status := jsonResult1["status"].(string)
if status == StatusCodeSuccess {
retVal2 = jsonResult1
} else {
infoCode := jsonResult1["infocode"].(string)
err2 = utils.NewErrorCode(jsonResult1["info"].(string), infoCode)
}
retVal = append(retVal, &tBatchAPIResponse{
Result: retVal2,
Err: err2,
})
}
}
errLevel = platformapi.ErrLevelSuccess
} else {
infoCode := jsonResult1["infocode"].(string)
err = utils.NewErrorCode(jsonResult1["info"].(string), infoCode)
if _, ok := exceedLimitCodes[infoCode]; ok {
errLevel = platformapi.ErrLevelExceedLimit
} else if _, ok := canRetryCodes[infoCode]; ok {
errLevel = platformapi.ErrLevelRecoverableErr
} else {
errLevel = platformapi.ErrLevelCodeIsNotOK
}
}
return errLevel, err
})
return retVal, err
}
// 为了方便调用者编码,如果失败,也会返回未转换的原始值
func (a *API) CoordinateConvert(lng, lat float64, coordsys string) (retLng, retLat float64, err error) {
// outCoords, err := a.BatchCoordinateConvert([]*Coordinate{
@@ -484,3 +567,29 @@ func (a *API) WalkingDistance(lng1, lat1, lng2, lat2 float64) (distance float64)
}
return distance
}
func (a *API) BatchWalkingDistance(srcLng, srcLat float64, destCoords []*Coordinate) (distanceList []float64, err error) {
var reqList []*tBatchAPIParams
for _, v := range destCoords {
reqList = append(reqList, &tBatchAPIParams{
APIStr: "direction/walking",
APIParams: map[string]interface{}{
"origin": coordinate2String(srcLng, srcLat),
"destination": coordinate2String(v.Lng, v.Lat),
},
})
}
resultList, err := a.BatchAccessAPI(reqList)
if err == nil {
for _, v := range resultList {
distance := float64(9527123)
if v.Err == nil {
if paths, _ := v.Result["route"].(map[string]interface{})["paths"].([]interface{}); len(paths) > 0 {
distance = utils.Interface2Float64WithDefault(paths[0].(map[string]interface{})["distance"], 0)
}
}
distanceList = append(distanceList, distance)
}
}
return distanceList, err
}