- GetCoordinateTownInfo
This commit is contained in:
@@ -179,11 +179,7 @@ func (a *API) CoordinateConvert(lng, lat float64, coordsys string) (retLng, retL
|
||||
|
||||
// 这里的District指的是实际的District,有些市是没有区的,比如东莞,这种情况下返回的区码是一个假的区域,即市的编码加上9000000
|
||||
func (a *API) GetCoordinateDistrictCode(lng, lat float64) (districtCode int) {
|
||||
params := map[string]interface{}{
|
||||
"location": fmt.Sprintf("%.6f,%.6f", lng, lat),
|
||||
}
|
||||
result, err := a.AccessAPI("geocode/regeo", params)
|
||||
// baseapi.SugarLogger.Debug(utils.Format4Output(result, false))
|
||||
result, err := a.GetCoordinateAreaInfo(lng, lat)
|
||||
if err == nil {
|
||||
addressComponent := result["regeocode"].(map[string]interface{})["addressComponent"].(map[string]interface{})
|
||||
if strAdcode, ok := addressComponent["adcode"].(string); ok {
|
||||
@@ -196,6 +192,25 @@ func (a *API) GetCoordinateDistrictCode(lng, lat float64) (districtCode int) {
|
||||
return districtCode
|
||||
}
|
||||
|
||||
func (a *API) GetCoordinateAreaInfo(lng, lat float64) (areaInfo map[string]interface{}, err error) {
|
||||
params := map[string]interface{}{
|
||||
"location": fmt.Sprintf("%.6f,%.6f", lng, lat),
|
||||
}
|
||||
result, err := a.AccessAPI("geocode/regeo", params)
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (a *API) GetCoordinateTownInfo(lng, lat float64) (townName, townCode string) {
|
||||
result, err := a.GetCoordinateAreaInfo(lng, lat)
|
||||
// baseapi.SugarLogger.Debug(utils.Format4Output(result, false))
|
||||
if err == nil {
|
||||
addressComponent := result["regeocode"].(map[string]interface{})["addressComponent"].(map[string]interface{})
|
||||
townName = utils.Interface2String(addressComponent["township"])
|
||||
townCode = utils.Interface2String(addressComponent["towncode"])
|
||||
}
|
||||
return townName, townCode
|
||||
}
|
||||
|
||||
// 这里的District指的是地点,不是实际上的区,具体级别看level
|
||||
// 这个函数返回的可能不是同级别的地点
|
||||
func (a *API) GetDistricts(subDistrict int, keywords string) (districtList []*District, err error) {
|
||||
|
||||
@@ -63,6 +63,15 @@ func TestGetCoordinateDistrictCode(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCoordinateTownInfo(t *testing.T) {
|
||||
dongguanLng := 120.982657
|
||||
dongguanLat := 31.410355
|
||||
townName, townCode := autonaviAPI.GetCoordinateTownInfo(dongguanLng, dongguanLat)
|
||||
if townName != "周市镇" || townCode != "320583102000" {
|
||||
t.Fatalf("townName:%s, townCode:%s", townName, townCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDistricts(t *testing.T) {
|
||||
districtList, err := autonaviAPI.GetDistricts(1, "东莞")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user