c# 通過經緯度查詢 具體的地址和地區名稱

來源:互聯網
上載者:User

最近項目需要通過經緯度查詢 具體的地址和地區名稱,通過查詢網路資源,發現提供的大多是得到具體的地址而對地區或城市名稱的擷取就不是很好把握;在這裡自己搞了個: 複製代碼 代碼如下://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就是擷取到的具體地址資訊和地區資訊;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.