一.根据地址解析坐标
1.官方文档地址
http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
2.c#封装方法
/// <summary>
/// 根据地址解析经纬度
/// </summary>
/// <param name="address">地址</param>
/// <param name="city">城市</param>
/// <returns></returns>
public static string GetMapLatOrLong(string address, string city)
{
string posturl = "http://api.map.baidu.com/geocoder?address=" + address + "&output=json&key=自己的key&city=" + city + "";
string postret = HttpGet(posturl);
//返回json数据:{ "status":"OK","result":{ "location":{ "lng":经度,"lat":纬度 },"precise":1,"confidence":75,"level":"\u5546\u52a1\u5927\u53a6"} }
string citylist = "";
JsonData jd = JsonMapper.ToObject(postret);
if (jd["status"].ToString() == "OK")
{
citylist = "{\"lng\":\""+ jd["result"]["location"]["lng"].ToString()
+ "\",\"lat\":\""+ jd["result"]["location"]["lat"].ToString()
+ "\",\"level\":\""+ jd["result"]["level"].ToString() + "\"}";
}
else
{
citylist = "{\"lng\":\"\",\"lat\":\"\",\"level\":\"\"}";
}
return citylist;
}
3.调用
string retcity= GetMapLatOrLong("地址","城市");
Response.Write(retcity);
二.根据坐标解析地址
1.官方文档地址
http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding-abroad
2.c#封装方法
/// <summary>
/// 按照定位的坐标,获取省市县
/// </summary>
/// <param name="lat"></param>
/// <param name="lng"></param>
/// <returns></returns>
public static string DecodeMapPostion(string lat, string lng)
{
string posturl = "http://api.map.baidu.com/geocoder/v2/?output=json&ak=自己的key&location=" + lat + "," + lng;
string postret =HttpGet(posturl);
string citylist = "";
JsonData jd = JsonMapper.ToObject(postret);
if (jd["status"].ToString() == "0")
{
//具体地址
string jtaddrs = jd["result"]["formatted_address"].ToString();
string bussinsaddrs = jd["result"]["sematic_description"].ToString();
citylist = "{\"province\":\"" + jd["result"]["addressComponent"]["province"].ToString() + "\",\"city\":\""
+ jd["result"]["addressComponent"]["city"].ToString() +
"\",\"area\":\"" + jd["result"]["addressComponent"]["district"].ToString() + "\",\"address\":\""
+ HtmlEncode(jtaddrs) + "\"}";
}
else
{
citylist = "{\"province\":\"\",\"city\":\"\",\"area\":\"\"}";
}
return citylist;
}
3.调用
string retcity= DecodeMapPostion(lat,lng);
Response.Write(retcity);