C#呼叫百度api,根據經度和緯度獲取地理位置資訊
/// <summary>
/// 百度api 根據經緯度獲取地理位置
/// </summary>
/// <param name="lng">經度</param>
/// <param name="lat">緯度</param>
/// <returns>具體的地理位置</returns>
public static string GetLocation( string lng,string lat)
{
HttpClient client = new HttpClient();
string url = string.Format("http://api.map.baidu.com/geocoder/v2/?ak=你的AK&callback=renderReverse&location={0},{1}&output=json&pois=1Z", lng,lat);
string result = client.GetStringAsync(url).Result;
var locationResult = (JObject)JsonConvert.DeserializeObject(result.Replace("renderReverse&&renderReverse","").Replace("(","").Replace(")",""));
if (locationResult == null || locationResult["result"] == null || locationResult["result"]["formatted_address"] == null)
return string.Empty;
var address = Convert.ToString(locationResult["result"]["formatted_address"]);
if (locationResult["result"]["sematic_description"] != null)
address += " " + Convert.ToString(locationResult["result"]["sematic_description"]);
return address;
}