-
Notifications
You must be signed in to change notification settings - Fork 0
/
coordtranslate.go
78 lines (66 loc) · 2.16 KB
/
coordtranslate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Created by GoLand.
* User: 姜伟
* Date: 2019/12/26 0026
* Time: 0:09
*/
package baidu
import (
"regexp"
"strings"
"github.com/a07061625/gompf/mpf/api/mpmap"
"github.com/a07061625/gompf/mpf/mpconstant/errorcode"
"github.com/a07061625/gompf/mpf/mperr"
"github.com/valyala/fasthttp"
)
type coordTranslate struct {
mpmap.BaseBaiDu
coords []string // 源坐标数组
fromType string // 源坐标类型
toType string // 目的坐标类型
}
func (ct *coordTranslate) AddCoord(lng string, lat string) {
if len(ct.coords) >= 100 {
panic(mperr.NewMapBaiDu(errorcode.MapBaiDuParam, "源坐标数量超过限制", nil))
}
match, _ := regexp.MatchString(`^[-]?(\d(\.\d+)?|[1-9]\d(\.\d+)?|1[0-7]\d(\.\d+)?|180)$`, lng)
if !match {
panic(mperr.NewMapBaiDu(errorcode.MapBaiDuParam, "源坐标经度不合法", nil))
}
match, _ = regexp.MatchString(`^[\-]?(\d(\.\d+)?|[1-8]\d(\.\d+)?|90)$`, lat)
if !match {
panic(mperr.NewMapBaiDu(errorcode.MapBaiDuParam, "源坐标纬度不合法", nil))
}
ct.coords = append(ct.coords, lng+","+lat)
}
func (ct *coordTranslate) SetFromType(fromType string) {
_, ok := coordTranslateTypes[fromType]
if ok {
ct.ReqData["from"] = fromType
} else {
panic(mperr.NewMapBaiDu(errorcode.MapBaiDuParam, "源坐标类型不合法", nil))
}
}
func (ct *coordTranslate) SetToType(toType string) {
_, ok := coordTranslateTypes[toType]
if ok {
ct.ReqData["to"] = toType
} else {
panic(mperr.NewMapBaiDu(errorcode.MapBaiDuParam, "目的坐标类型不合法", nil))
}
}
func (ct *coordTranslate) CheckData() (*fasthttp.Client, *fasthttp.Request) {
if len(ct.coords) == 0 {
panic(mperr.NewMapBaiDu(errorcode.MapBaiDuParam, "源坐标不能为空", nil))
}
ct.ReqData["coords"] = strings.Join(ct.coords, ";")
return ct.GetRequest()
}
func NewCoordTranslate() *coordTranslate {
ct := &coordTranslate{mpmap.NewBaseBaiDu(), make([]string, 0), "", ""}
ct.SetServiceUri("/geoconv/v1/")
ct.SetRespTag("result")
ct.ReqData["from"] = "1"
ct.ReqData["to"] = "5"
return ct
}