需要自己去百度开发者平台申请key
http://lbsyun.baidu.com/index.php?title=%E9%A6%96%E9%A1%B5
/// <summary>
/// 根据经纬度获取地理位置
/// </summary>
/// <param name="lat">纬度</param>
/// <param name="lng">经度</param>
/// <returns>具体的地埋位置</returns>
public static VmAddress GetLocation(double lat, double lng)
{
//百度api
string url = @"http://api.map.baidu.com/geocoder/v2/?location={0}&output=json&ak=你的key";
HttpClient client = new HttpClient();
string location = string.Format("{0},{1}", lat, lng);
string bdUrl = string.Format(url, location);
string result = client.GetStringAsync(bdUrl).Result;
var locationResult = (JObject)JsonConvert.DeserializeObject(result);
//
if (locationResult == null || locationResult["result"] == null || locationResult["result"]["formatted_address"] == null)
return null;
var address = Convert.ToString(locationResult["result"]["formatted_address"]);
if (locationResult["result"]["sematic_description"] != null)
address += " " + Convert.ToString(locationResult["result"]["sematic_description"]);
var privince = Convert.ToString(locationResult["result"]["addressComponent"]["province"]);
var city = Convert.ToString(locationResult["result"]["addressComponent"]["city"]);
//到这里省市县都可以拿得到了,由于我只需要省市,其它的就没有要了,可以从locationResult中取出来
VmAddress vmAddress = new VmAddress()
{
Privince = privince,
City = city,
Address = address
};
return vmAddress;
}