public M_gaode_addressComponent getaddressbylocation2(string location)
{
M_gaode_addressComponent returnaddress =null;
string url = string.Format("https://restapi.amap.com/v3/geocode/regeo?location={0}&poitype=区县级地名&output=JSON&key={1}&radius=1000&extensions=all", location, gaode_key);
try
{
string result = HttpHelper.Get(url);
if (!string.IsNullOrWhiteSpace(result))
{
//json解析
M_Gaode_Model<M_gaode_regeocode> obj = JsonConvert.DeserializeObject<M_Gaode_Model<M_gaode_regeocode>>(result);
if (obj != null)
{
if (obj.regeocode != null && obj.regeocode.addressComponent != null)
{
var info = obj.regeocode.addressComponent;
if (info.province.ToString().Equals("[]"))
{
info.city = string.Empty;
}
if (info.city.ToString().Equals("[]"))
{
info.city = string.Empty;
}
if (info.district.ToString().Equals("[]"))
{
info.district = string.Empty;
}
return info;
}
}
LogHelper.WriteBarcodesLog(string.Format("getaddressbylocation:{0}|{1}", location, result));
}
}
catch (Exception ex)
{
LogHelper.WriteBarcodesLog(string.Format("getaddressbylocation:{0}|{1}", location, ex.Message));
}
return returnaddress;
}
实体类:
public class M_gaode_addressComponent
{
public object country { get; set; }
public object province { get; set; }
public object city { get; set; }
public object district { get; set; }
public object adcode { get; set; }
public object township { get; set; }
public object towncode { get; set; }
}
实例:
var addressinfo = gd.getaddressbylocation2(106.45857 + "," + 29.50645);
展示效果: