1.根据地名获取经纬度
使用百度地图正/逆地理编码服务
方法:
public async Task<string> GetData()
{
string key = "*******";
string api = $"https://api.map.baidu.com/geocoding/v3/?address=彩讯大厦&output=json&ak={key}";
HttpClient hc = new HttpClient();
var responseBody = await hc.GetStringAsync(api);
JObject jsonObj = JObject.Parse(responseBody);
string lng = jsonObj["result"]["location"]["lng"].ToString();
string lat = jsonObj["result"]["location"]["lat"].ToString();
return lng + lat;
}
这里没引用microsoft.aspnetcore.mvc.newtonsoftjson包会报错
调用:
[HttpGet("[action]")]
[Public]
public async Task<IActionResult> GetData()
{
var vm = Wtm.CreateVM<FarmerImportVM>();
var message = await vm.GetData(); //上面用了await使方法变成同步,这里也要用await保持一致,否则会报错
return Ok(ResponseBody.SetReponseInfo(new { data = message }, null, true, ""));
}
2.高德地图获取省-市-县(区)-乡镇数据
public async Task<List<string>> GetData()
{
string key = "**************************";
int subdistrict = 1; //子级行政区(如:0不返回下级行政区,1返回下一级行政区,2返回下两级行政区,3返回下三级行政区)
string api = $"https://restapi.amap.com/v3/config/district?key={key}&keywords=广东省&subdistrict={subdistrict}";
HttpClient hc = new HttpClient();
var responseBody = await hc.GetStringAsync(api);
JObject jsonObj = JObject.Parse(responseBody);
List<string> cityName = new List<string>();
var children = jsonObj["districts"][0]["districts"];
cityName.Add(jsonObj["districts"][0]["name"].ToString());
for (int i = 0; i < 3; i++)
{
cityName.Add(children[i]["name"].ToString());
}
return cityName;
}