- up
This commit is contained in:
@@ -177,6 +177,27 @@ func (a *API) CoordinateConvert(lng, lat float64, coordsys string) (retLng, retL
|
||||
return lng, lat, err
|
||||
}
|
||||
|
||||
func (a *API) GetCoordinateFromAddress(address string, cityInfo string) (lng, lat float64, districtCode int) {
|
||||
params := map[string]interface{}{
|
||||
"address": address,
|
||||
}
|
||||
if cityInfo != "" {
|
||||
params["city"] = cityInfo
|
||||
}
|
||||
|
||||
result, err := a.AccessAPI("geocode/geo", params)
|
||||
if err == nil {
|
||||
if geocodes, ok := result["geocodes"].([]interface{}); ok && len(geocodes) > 0 {
|
||||
geocode := geocodes[0].(map[string]interface{})
|
||||
locationList := strings.Split(utils.Interface2String(geocode["location"]), ",")
|
||||
if len(locationList) > 1 {
|
||||
return utils.Str2Float64WithDefault(locationList[0], 0), utils.Str2Float64WithDefault(locationList[1], 0), int(utils.Str2Int64(utils.Interface2String(geocode["adcode"])))
|
||||
}
|
||||
}
|
||||
}
|
||||
return lng, lat, 0
|
||||
}
|
||||
|
||||
// 这里的District指的是实际的District,有些市是没有区的,比如东莞,这种情况下返回的区码是一个假的区域,即市的编码加上9000000
|
||||
func (a *API) GetCoordinateDistrictCode(lng, lat float64) (districtCode int) {
|
||||
result, err := a.GetCoordinateAreaInfo(lng, lat)
|
||||
|
||||
@@ -83,3 +83,8 @@ func TestGetDistricts(t *testing.T) {
|
||||
}
|
||||
t.Log(utils.Format4Output(districtList, false))
|
||||
}
|
||||
|
||||
func TestGetCoordinateFromAddress(t *testing.T) {
|
||||
lng, lat, districtCode := autonaviAPI.GetCoordinateFromAddress("天府广场", "成都市")
|
||||
t.Logf("lng:%f, lat:%f, districtCode:%d", lng, lat, districtCode)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user