namespace 通过坐标获得地址
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetAddressByLatLng(39.910093, 116.403945));
Console.ReadKey();
}
public static string GetAddressByLatLng(double lat, double lng)
{
WebClient client = new WebClient();//webclient客户端对象
//string url = "http://maps.google.com/maps/api/geocode/xml?latlng=39.910093,116.403945&language=zh-CN&sensor=false";//请求地址
//http://maps.google.com/maps/api/geocode/xml?latlng=39.910093,116.403945&language=zh-CN&sensor=false
string url = "http://maps.google.com/maps/api/geocode/xml?latlng=" + lat + "," + lng + "&language=zh-CN&sensor=false";//请求地址
client.Encoding = Encoding.UTF8;//编码格式
string responseTest = client.DownloadString(url);//下载xml响应数据
XmlDocument doc = new XmlDocument();//创建XML文档对象
string address = "地址信息解析失败!";
if (!string.IsNullOrEmpty(responseTest))
{
doc.LoadXml(responseTest);//加载xml字符串
//获取状态信息
string xpath = @"GeocodeResponse/status";
XmlNode node = doc.SelectSingleNode(xpath);
string status = node.InnerText.ToString();
if (status == "OK")
{
//获取地址信息
xpath = @"GeocodeResponse/result/formatted_address";
node = doc.SelectSingleNode(xpath);
address = node.InnerText.ToString();
}
}
return address;//输出地址信息
}
}
}
引用这里的http://www.cnblogs.com/liuhaorain/archive/2012/01/31/2334018.html看看是不是你要的 |