+ BatchCoordinateConvert

This commit is contained in:
gazebo
2019-08-21 21:27:55 +08:00
parent c91dfdeb21
commit 6b1f4d6aef

View File

@@ -43,6 +43,7 @@ const (
const (
FakeDistrictPadding = 9000000
MaxConvertCount = 40
)
var (
@@ -89,6 +90,11 @@ type District struct {
Districts []*District `json:"districts"`
}
type Coordinate struct {
Lng float64 `json:"lng"`
Lat float64 `json:"lat"`
}
type ResponseResult map[string]interface{}
type API struct {
@@ -161,6 +167,15 @@ func (a *API) AccessAPI(apiStr string, params map[string]interface{}) (retVal Re
// 为了方便调用者编码,如果失败,也会返回未转换的原始值
func (a *API) CoordinateConvert(lng, lat float64, coordsys string) (retLng, retLat float64, err error) {
// outCoords, err := a.BatchCoordinateConvert([]*Coordinate{
// &Coordinate{
// Lng: lng,
// Lat: lat,
// },
// }, coordsys)
// if err == nil {
// retLng, retLat = outCoords[0].Lng, outCoords[0].Lat
// }
if coordsys == "" || coordsys == CoordSysAutonavi {
return lng, lat, nil
}
@@ -180,6 +195,33 @@ func (a *API) CoordinateConvert(lng, lat float64, coordsys string) (retLng, retL
return lng, lat, err
}
func (a *API) BatchCoordinateConvert(coords []*Coordinate, coordsys string) (outCoords []*Coordinate, err error) {
if coordsys == "" || coordsys == CoordSysAutonavi {
return coords, nil
}
var coordsStrList []string
for _, v := range coords {
coordsStrList = append(coordsStrList, fmt.Sprintf("%.6f,%.6f", v.Lng, v.Lat))
}
params := map[string]interface{}{
"locations": strings.Join(coordsStrList, "|"),
"coordsys": coordsys,
}
result, err := a.AccessAPI("assistant/coordinate/convert", params)
if err == nil {
coordinate := result["locations"].(string)
retCoordsStrList := strings.Split(coordinate, ";")
for _, v := range retCoordsStrList {
pair := strings.Split(v, ",")
outCoords = append(outCoords, &Coordinate{
Lng: utils.Str2Float64(pair[0]),
Lat: utils.Str2Float64(pair[1]),
})
}
}
return outCoords, err
}
func (a *API) GetCoordinateFromAddress(address string, cityInfo string) (lng, lat float64, districtCode int) {
params := map[string]interface{}{
"address": address,