• C#根据经纬度获取物理地址


    调用命名空间:

    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是否可以得到数据。

  • 相关阅读:
    synchronized底层实现学习
    [Alink漫谈之三] AllReduce通信模型
    Alink漫谈(二) : 从源码看机器学习平台Alink设计和架构
    Alink漫谈(一) : 从KMeans算法实现不同看Alink设计思想
    [源码分析]从"UDF不应有状态" 切入来剖析Flink SQL代码生成 (修订版)
    从"UDF不应有状态" 切入来剖析Flink SQL代码生成
    [源码分析] 带你梳理 Flink SQL / Table API内部执行流程
    [白话解析] 通俗解析集成学习之bagging,boosting & 随机森林
    [源码分析] 从FlatMap用法到Flink的内部实现
    Ceph 14.2.5-K8S 使用Ceph存储实战 -- <6>
  • 原文地址:https://www.cnblogs.com/bingsying/p/7846724.html
Copyright © 2020-2023  润新知