C#根據經緯度擷取物理地址

來源:互聯網
上載者:User

標籤:array   des   count   location   根據   response   request   text   mat   

調用命名空間:

using System.Net;
using System.IO;
using System.Xml;

 

調用案例:

private string GetAddress(string lng, string lat) 

{

      try

            {

                string url = @"http://api.map.baidu.com/geocoder/v2/?ak=oGdNg8wTEMEIsC0uzqqD3nq0&callback=renderReverse&location=" + lat + "," + lng + @"&output=xml&pois=1";

                WebRequest request = WebRequest.Create(url);

                request.Method = "POST";

                XmlDocument xmlDoc = new XmlDocument();

                string sendData = xmlDoc.InnerXml;

                byte[] byteArray = Encoding.Default.GetBytes(sendData);

                Stream dataStream = request.GetRequestStream();

                dataStream.Write(byteArray, 0, byteArray.Length);

                dataStream.Close();

                WebResponse response = request.GetResponse();

                dataStream = response.GetResponseStream();

                StreamReader reader = new StreamReader(dataStream, System.Text.Encoding.GetEncoding("utf-8"));

                string responseXml = reader.ReadToEnd();

                XmlDocument xml = new XmlDocument();

                xml.LoadXml(responseXml);

                string status = xml.DocumentElement.SelectSingleNode("status").InnerText;                                         if (status == "0")

                {

                    XmlNodeList nodes = xml.DocumentElement.GetElementsByTagName("formatted_address");

                    if (nodes.Count > 0)

                    {

                        return nodes[0].InnerText;

                    }

                    else

                         return "未擷取到位置資訊,錯誤碼3";

                    }

                else

                {

                    return "未擷取到位置資訊,錯誤碼1";

                }

            }

            catch (System.Exception ex)

            {

                return "未擷取到位置資訊,錯誤碼" + ex + "";

            }

 }

 

---注意:

    當status=“0”但nodes[0].InnerText結果依然為null時,在網址上查看url是否可以得到資料。

C#根據經緯度擷取物理地址

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.