最近项目需要通过经纬度查询 具体的地址和区域名称,通过查询网络资源,发现提供的大多是得到具体的地址而对区域或城市名称的获取就不是很好把握;在这里自己搞了个:
//webclient客户端对象 WebClient client = new WebClient(); string url = "http://maps.google.com/maps/api/geocode/xml?latlng=" + latitude + "," + longitude + "&language=zh-CN&sensor=false";//请求地址 client.Encoding = Encoding.UTF8;//编码格式 string responseTest = client.DownloadString(url); //下载xml响应数据 string address = "";//返回的地址 XmlDocument doc = new XmlDocument(); //创建XML文档对象 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(); //查询地区信息 XmlNodeList nodeListAll = doc.SelectNodes("GeocodeResponse/result"); XmlNode idt = nodeListAll[0]; XmlNodeList idts = idt.SelectNodes("address_component[type='sublocality']"); //address_component[type='sublocality']表示筛选type='sublocality'的所有相关子节点; XmlNode idtst = idts[0]; string area = idtst.SelectSingleNode("short_name").InnerText; address = address + "," + area; } }
address就是获取到的具体地址信息和区域信息;