1
This commit is contained in:
@@ -3,10 +3,12 @@ package baidunavi
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
@@ -16,11 +18,14 @@ const (
|
||||
signKey = "sn"
|
||||
resultKey = "result"
|
||||
prodURL = "http://api.map.baidu.com"
|
||||
prodURL2 = "https://api.map.baidu.com"
|
||||
)
|
||||
|
||||
const (
|
||||
StatusCodeSuccess = 0
|
||||
StatusCodeInternalErr = 1 // 服务器内部错误
|
||||
StatusCodeParamsErr = 2 //参数无效
|
||||
StatusCodeNoReturn = 7 //无返回结果
|
||||
StatusCodeExceedDailyQuota = 301 // 永久配额超限,限制访问
|
||||
StatusCodeExceedQuota = 302 // 天配额超限,限制访问
|
||||
StatusCodeExceedDailyConcurrentQuota = 401 // 当前并发量已经超过约定并发配额,限制访问
|
||||
@@ -55,6 +60,46 @@ type Coordinate struct {
|
||||
Lat float64 `json:"y"`
|
||||
}
|
||||
|
||||
type Coordinate2 struct {
|
||||
Lng string `json:"lng"`
|
||||
Lat string `json:"lat"`
|
||||
}
|
||||
|
||||
type RidingResp struct {
|
||||
Status int `json:"status"` //状态码
|
||||
Message string `json:"message"` // 状态码对应的信息
|
||||
Result struct {
|
||||
Origin struct { //起点
|
||||
Lng float64 `json:"lng"`
|
||||
Lat float64 `json:"lat"`
|
||||
}
|
||||
Destination struct { //终点
|
||||
Lng float64 `json:"lng"`
|
||||
Lat float64 `json:"lat"`
|
||||
}
|
||||
Routers struct {
|
||||
Distance float64 `json:"distance"` //方案距离,单位:米
|
||||
Duration int `json:"duration"` //线路耗时,单位:秒
|
||||
Steps []struct {
|
||||
}
|
||||
Name string `json:"name"` //道路名称
|
||||
Instruction string `json:"instruction"` //路段描述
|
||||
StartLocation struct {
|
||||
Lng float64 `json:"lng"`
|
||||
Lat float64 `json:"lat"`
|
||||
}
|
||||
EndLocation struct {
|
||||
Lng float64 `json:"lng"`
|
||||
Lat float64 `json:"lat"`
|
||||
}
|
||||
Path []struct {
|
||||
Lng float64 `json:"lng"`
|
||||
Lat float64 `json:"lat"`
|
||||
}
|
||||
}
|
||||
} `json:"result"`
|
||||
}
|
||||
|
||||
type API struct {
|
||||
client *http.Client
|
||||
config *platformapi.APIConfig
|
||||
@@ -121,7 +166,8 @@ func (a *API) AccessAPI(apiStr string, params map[string]interface{}) (retVal in
|
||||
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||
func() *http.Request {
|
||||
request, _ := http.NewRequest(http.MethodGet, genGetURL(prodURL, apiStr, params2), nil)
|
||||
//request, _ := http.NewRequest(http.MethodGet, genGetURL(prodURL, apiStr, params2), nil)
|
||||
request, _ := http.NewRequest(http.MethodGet, genGetURL(prodURL2, apiStr, params2), nil)
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
@@ -165,3 +211,55 @@ func (a *API) BatchCoordinateConvert(coords []*Coordinate, fromCoordSys, toCoord
|
||||
}
|
||||
return outCoords, err
|
||||
}
|
||||
|
||||
// DirectionLiteRide 骑行路线规划
|
||||
func (a *API) DirectionLiteRide(coords []*Coordinate) (retVal interface{}, err error) {
|
||||
var (
|
||||
sCoords string
|
||||
uCoords string
|
||||
timestamp = time.Now().Unix()
|
||||
apiStr = "directionlite/v1/riding"
|
||||
)
|
||||
sCoords = utils.Float64ToStr(coords[0].Lat) + "," + utils.Float64ToStr(coords[0].Lng)
|
||||
uCoords = utils.Float64ToStr(coords[1].Lat) + "," + utils.Float64ToStr(coords[1].Lng)
|
||||
|
||||
param := map[string]interface{}{
|
||||
"origin": sCoords,
|
||||
"destination": uCoords,
|
||||
"timestamp": timestamp,
|
||||
//"steps_info": 2,
|
||||
}
|
||||
//生成签名
|
||||
params2 := utils.MergeMaps(utils.Params2Map("ak", a.ak, "output", "json"), param)
|
||||
sn := a.signParams(apiStr, params2)
|
||||
|
||||
params := url.Values{
|
||||
"origin": []string{sCoords},
|
||||
"destination": []string{uCoords},
|
||||
"ak": []string{a.ak},
|
||||
"sn": []string{sn},
|
||||
"timestamp": []string{utils.Int64ToStr(timestamp)},
|
||||
}
|
||||
|
||||
// 发起请求
|
||||
request, err := url.Parse(prodURL2 + "/" + apiStr + "?" + params.Encode())
|
||||
if nil != err {
|
||||
fmt.Printf("host error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err1 := http.Get(request.String())
|
||||
fmt.Printf("url: %s\n", request.String())
|
||||
defer resp.Body.Close()
|
||||
if err1 != nil {
|
||||
fmt.Printf("request error: %v", err1)
|
||||
return
|
||||
}
|
||||
body, err2 := ioutil.ReadAll(resp.Body)
|
||||
if err2 != nil {
|
||||
fmt.Printf("response error: %v", err2)
|
||||
}
|
||||
result := string(body)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package baidunavi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
@@ -19,7 +20,8 @@ func init() {
|
||||
sugarLogger = logger.Sugar()
|
||||
baseapi.Init(sugarLogger)
|
||||
|
||||
api = New("eL94zToVOdGDTkNQxV8dnEQ1ZRcB2UKb", "ZG0OOpOsOVURUwAkkmoHQFKRCbzn0zGb")
|
||||
//api = New("eL94zToVOdGDTkNQxV8dnEQ1ZRcB2UKb", "ZG0OOpOsOVURUwAkkmoHQFKRCbzn0zGb")
|
||||
api = New("wW2AwzPS0hdaPy5QLalzso7ARX5uYZtZ", "ZG0OOpOsOVURUwAkkmoHQFKRCbzn0zGb")
|
||||
}
|
||||
|
||||
func TestBatchCoordinateConvert(t *testing.T) {
|
||||
@@ -39,3 +41,20 @@ func TestBatchCoordinateConvert(t *testing.T) {
|
||||
t.Log(utils.Format4Output(result, false))
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectionLiteRide(t *testing.T) {
|
||||
result, err := api.DirectionLiteRide([]*Coordinate{
|
||||
{
|
||||
Lng: 104.063285,
|
||||
Lat: 30.571255,
|
||||
},
|
||||
&Coordinate{
|
||||
Lng: 104.065132,
|
||||
Lat: 30.610506,
|
||||
},
|
||||
})
|
||||
|
||||
fmt.Println(err)
|
||||
fmt.Println(result)
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package mtwmapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
@@ -17,3 +18,11 @@ func TestGetRefundSkuDetailFromMsg(t *testing.T) {
|
||||
result := api.GetRefundSkuDetailFromMsg(msg)
|
||||
t.Log(utils.Format4Output(result, false))
|
||||
}
|
||||
|
||||
func TestSplice(t *testing.T) {
|
||||
var params = map[string]interface{}{}
|
||||
var statuss []int
|
||||
params["statuss"] = "[20]"
|
||||
utils.UnmarshalUseNumber([]byte(params["statuss"].(string)), &statuss)
|
||||
fmt.Println(statuss)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user