.net根据经纬度获取地址(百度api)

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 "未获取到位置信息,错误码2";
            }
        }



你可能感兴趣的:(visual,studio,2015,Xamarin)