You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package biz
import (
"encoding/json"
"errors"
"fmt"
"github.com/go-kratos/kratos/v2/log"
"io"
"net/http"
)
type MapServiceBiz struct {
log *log.Helper
}
func NewMapServiceBiz(logger log.Logger) *MapServiceBiz {
return &MapServiceBiz{log: log.NewHelper(logger)}
}
// 获取驾驶信息
func (msbiz *MapServiceBiz) GetDriverInfo(origin, destination string) (string, string, error) {
// 一,请求获取
key := "54be816ba4fae6572a1701f2c578ec71"
api := "https://restapi.amap.com/v3/direction/driving"
parameters := fmt.Sprintf("origin=%s&destination=%s&extensions=base&output=json&key=%s", origin, destination, key)
url := api + "?" + parameters
resp, err := http.Get(url)
if err != nil {
return "", "", err
}
defer func() {
_ = resp.Body.Close()
}()
body, err := io.ReadAll(resp.Body) // resp.Body是io.Reader类型 需要io.ReadAll读出来变成byte[]类型
if err != nil {
return "", "", err
}
// 二,解析出来,json
ddResp := DirectionDrivingResp{}
if err := json.Unmarshal(body, &ddResp); err != nil {
return "", "", err
}
// 三判定LSB请求结果
if ddResp.Status == "0" {
return "", "", errors.New(ddResp.Info)
}
// 四,正确返回,默认使用第一条路线
path := ddResp.Route.Paths[0]
return path.Distance, path.Duration, nil
}
type DirectionDrivingResp struct {
Status string `json:"status,omitempty"`
Info string `json:"info,omitempty"`
Infocode string `json:"infocode,omitempty"`
Count string `json:"count,omitempty"`
Route struct {
Origin string `json:"origin,omitempty"`
Destination string `json:"destination,omitempty"`
Paths []Path `json:"paths,omitempty"`
} `json:"route"`
}
type Path struct {
Distance string `json:"distance,omitempty"`
Duration string `json:"duration,omitempty"`
Strategy string `json:"strategy,omitempty"`
}