using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Globalization; using System.Net; using System.IO; using System.Configuration; using System.Xml; namespace WSGPSGateway.BLL { public class LocationAPI { private static string GeoCodeUrl = ConfigurationManager.AppSettings["GeoCodeUrl"]; private static string GoogleGearUrl = ConfigurationManager.AppSettings["GoogleGearUrl"]; private static string UseGoogleReverse = ConfigurationManager.AppSettings["UseGoogleReverse"]; /// <summary> /// Initializes a new instance of the <see cref="LocationAPI"/> class. /// </summary> public LocationAPI() { } public static string GetAddressFromLL(string Lat, string Lng) { string url = "http://ws.geonames.org/findNearestAddress?lat=" + Lat + "&lng=" + Lng; WebResponse response = null; Stream stream = null; StreamReader reader = null; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.40607; .NET CLR 1.1.4322)"; request.Timeout = 1; response = request.GetResponse(); stream = response.GetResponseStream(); reader = new StreamReader(stream, System.Text.Encoding.UTF8); XmlDocument xDoc = new XmlDocument(); xDoc.Load(stream); XmlNode xStreet = xDoc.SelectSingleNode("geonames/address/street"); XmlNode xStreetNumber = xDoc.SelectSingleNode("geonames/address/streetNumber"); XmlNode xPlacename = xDoc.SelectSingleNode("geonames/address/placename"); XmlNode xCountryCode = xDoc.SelectSingleNode("geonames/address/countryCode"); string address = xStreet.InnerText + " " + xStreetNumber.InnerText + ", " + xPlacename.InnerText + ", " + xCountryCode.InnerText + " "; return address; } catch(Exception ex) { DAL.Log.Write(DateTime.Now.ToString() + "\0" + ex.Message.ToString() + "\r\n"); return "UnKnown"; } finally { if (reader != null) reader.Close(); if (stream != null) stream.Close(); if (response != null) response.Close(); } } } }
C#根据经纬度请求返回坐标实际地址
最新推荐文章于 2021-08-12 09:03:17 发布