c# 通過經(jīng)緯度查詢 具體的地址和區(qū)域名稱
更新時(shí)間:2012年11月21日 11:11:55 作者:
最近項(xiàng)目需要通過經(jīng)緯度查詢 具體的地址和區(qū)域名稱,通過查詢網(wǎng)絡(luò)資源,發(fā)現(xiàn)提供的大多是得到具體的地址而對區(qū)域或城市名稱的獲取就不是很好把握;在這里自己搞了個(gè),需要的朋友可以參考下
最近項(xiàng)目需要通過經(jīng)緯度查詢 具體的地址和區(qū)域名稱,通過查詢網(wǎng)絡(luò)資源,發(fā)現(xiàn)提供的大多是得到具體的地址而對區(qū)域或城市名稱的獲取就不是很好把握;在這里自己搞了個(gè):
//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響應(yīng)數(shù)據(jù)
string address = "";//返回的地址
XmlDocument doc = new XmlDocument();
//創(chuàng)建XML文檔對象
if (!string.IsNullOrEmpty(responseTest))
{
doc.LoadXml(responseTest);//加載xml字符串
//查詢狀態(tài)信息
string xpath = @"GeocodeResponse/status";
XmlNode node = doc.SelectSingleNode(xpath);
string status = node.InnerText.ToString();
if (status == "OK") {
//查詢詳細(xì)地址信息
xpath = @"GeocodeResponse/result/formatted_address";
node = doc.SelectSingleNode(xpath);
address = node.InnerText.ToString();
//查詢地區(qū)信息
XmlNodeList nodeListAll = doc.SelectNodes("GeocodeResponse/result");
XmlNode idt = nodeListAll[0];
XmlNodeList idts = idt.SelectNodes("address_component[type='sublocality']");
//address_component[type='sublocality']表示篩選type='sublocality'的所有相關(guān)子節(jié)點(diǎn);
XmlNode idtst = idts[0];
string area = idtst.SelectSingleNode("short_name").InnerText;
address = address + "," + area;
}
}
address就是獲取到的具體地址信息和區(qū)域信息;
復(fù)制代碼 代碼如下:
//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響應(yīng)數(shù)據(jù)
string address = "";//返回的地址
XmlDocument doc = new XmlDocument();
//創(chuàng)建XML文檔對象
if (!string.IsNullOrEmpty(responseTest))
{
doc.LoadXml(responseTest);//加載xml字符串
//查詢狀態(tài)信息
string xpath = @"GeocodeResponse/status";
XmlNode node = doc.SelectSingleNode(xpath);
string status = node.InnerText.ToString();
if (status == "OK") {
//查詢詳細(xì)地址信息
xpath = @"GeocodeResponse/result/formatted_address";
node = doc.SelectSingleNode(xpath);
address = node.InnerText.ToString();
//查詢地區(qū)信息
XmlNodeList nodeListAll = doc.SelectNodes("GeocodeResponse/result");
XmlNode idt = nodeListAll[0];
XmlNodeList idts = idt.SelectNodes("address_component[type='sublocality']");
//address_component[type='sublocality']表示篩選type='sublocality'的所有相關(guān)子節(jié)點(diǎn);
XmlNode idtst = idts[0];
string area = idtst.SelectSingleNode("short_name").InnerText;
address = address + "," + area;
}
}
address就是獲取到的具體地址信息和區(qū)域信息;
相關(guān)文章
可替代log4j日志的c#簡單日志類隊(duì)列實(shí)現(xiàn)類代碼分享
簡單日志類隊(duì)列實(shí)現(xiàn)??砂刺熘茉履甏笮》指钗募?珊唵翁娲鷏og4j2013-12-12
C#迭代器模式(Iterator Pattern)實(shí)例教程
這篇文章主要介紹了C#迭代器模式(Iterator Pattern),包括了迭代器的適用范圍及用法實(shí)例,需要的朋友可以參考下2014-09-09
C#科學(xué)繪圖之使用scottPlot繪制多個(gè)圖像
ScottPlot是基于.Net的一款開源免費(fèi)的交互式可視化庫,支持Winform和WPF等UI框架,本文主要為大家詳細(xì)介紹了如何使用scottPlot實(shí)現(xiàn)繪制多個(gè)圖像,需要的可以參考下2023-12-12
基于C#解決庫存扣減及訂單創(chuàng)建時(shí)防止并發(fā)死鎖的問題
這篇文章主要介紹了基于C#解決庫存扣減及訂單創(chuàng)建時(shí)防止并發(fā)死鎖的問題,很多開發(fā)人員對于這個(gè)問題的排查起來是比較困難的,而生產(chǎn)生的原因多種多樣,很多人認(rèn)是因?yàn)楸碇械臄?shù)據(jù)太多了同時(shí)操作的人多人才會(huì)產(chǎn)生這種錯(cuò)誤,下面我們來還原一下死鎖的過程2022-05-05
Unity性能優(yōu)化Shader函數(shù)ShaderUtil.GetShaderGlobalKeywords用法示例
這篇文章主要為大家介紹了Unity性能優(yōu)化Shader函數(shù)ShaderUtil.GetShaderGlobalKeywords用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

