- autonavi.GetCoordinateDistrictCode will return fake district code when the city has no district.
This commit is contained in:
@@ -41,6 +41,10 @@ const (
|
|||||||
CoordSysBaidu = "baidu"
|
CoordSysBaidu = "baidu"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
FakeDistrictPadding = 9000000
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
exceedLimitCodes = map[string]int{
|
exceedLimitCodes = map[string]int{
|
||||||
InfoCode_ACCESS_TOO_FREQUENT: 1,
|
InfoCode_ACCESS_TOO_FREQUENT: 1,
|
||||||
@@ -164,6 +168,7 @@ func (a *API) CoordinateConvert(lng, lat float64, coordsys string) (retLng, retL
|
|||||||
return lng, lat, err
|
return lng, lat, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 有些市是没有区的,比如东莞,这种情况下返回的区码是一个假的区域,即市的编码加上9000000
|
||||||
func (a *API) GetCoordinateDistrictCode(lng, lat float64) (districtCode int) {
|
func (a *API) GetCoordinateDistrictCode(lng, lat float64) (districtCode int) {
|
||||||
params := map[string]interface{}{
|
params := map[string]interface{}{
|
||||||
"location": fmt.Sprintf("%.6f,%.6f", lng, lat),
|
"location": fmt.Sprintf("%.6f,%.6f", lng, lat),
|
||||||
@@ -174,6 +179,9 @@ func (a *API) GetCoordinateDistrictCode(lng, lat float64) (districtCode int) {
|
|||||||
addressComponent := result["regeocode"].(map[string]interface{})["addressComponent"].(map[string]interface{})
|
addressComponent := result["regeocode"].(map[string]interface{})["addressComponent"].(map[string]interface{})
|
||||||
if strAdcode, ok := addressComponent["adcode"].(string); ok {
|
if strAdcode, ok := addressComponent["adcode"].(string); ok {
|
||||||
districtCode = int(utils.Str2Int64WithDefault(strAdcode, 0))
|
districtCode = int(utils.Str2Int64WithDefault(strAdcode, 0))
|
||||||
|
if _, ok := addressComponent["district"].(string); !ok {
|
||||||
|
districtCode += FakeDistrictPadding
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return districtCode
|
return districtCode
|
||||||
|
|||||||
@@ -37,13 +37,24 @@ func TestCoordinateConvert(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetCoordinateCodes(t *testing.T) {
|
func TestGetCoordinateCodes(t *testing.T) {
|
||||||
// districtCode := autonaviAPI.GetCoordinateDistrictCode(119.40695, 32.38755)
|
dongguanLng := 113.703870
|
||||||
districtCode := autonaviAPI.GetCoordinateDistrictCode(104.053567, 30.576072)
|
dongguanLat := 22.833210
|
||||||
|
districtCode := autonaviAPI.GetCoordinateDistrictCode(dongguanLng, dongguanLat)
|
||||||
|
if districtCode != 9441900 {
|
||||||
|
t.Fatalf("TestGetCoordinateCodes failed")
|
||||||
|
} else {
|
||||||
|
t.Log(districtCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
cdgxWuhouLng := 104.052971
|
||||||
|
cdgxWuhouLat := 30.576076
|
||||||
|
districtCode = autonaviAPI.GetCoordinateDistrictCode(cdgxWuhouLng, cdgxWuhouLat)
|
||||||
if districtCode != 510107 {
|
if districtCode != 510107 {
|
||||||
t.Fatalf("TestGetCoordinateCodes failed")
|
t.Fatalf("TestGetCoordinateCodes failed")
|
||||||
} else {
|
} else {
|
||||||
t.Log(districtCode)
|
t.Log(districtCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
districtCode = autonaviAPI.GetCoordinateDistrictCode(0, 0)
|
districtCode = autonaviAPI.GetCoordinateDistrictCode(0, 0)
|
||||||
if districtCode != 0 {
|
if districtCode != 0 {
|
||||||
t.Fatalf("TestGetCoordinateCodes failed")
|
t.Fatalf("TestGetCoordinateCodes failed")
|
||||||
|
|||||||
Reference in New Issue
Block a user