根据经度和纬度返回地理位置 LocationAPi

本文介绍了一个使用Geonames API从经纬度坐标获取详细地址的方法。通过构造HTTP请求并解析XML响应来实现地理逆向定位的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

View Code
publicclass LocationAPI
{
privatestaticstring GeoCodeUrl = ConfigurationManager.AppSettings["GeoCodeUrl"];
privatestaticstring GoogleGearUrl = ConfigurationManager.AppSettings["GoogleGearUrl"];
privatestaticstring UseGoogleReverse = ConfigurationManager.AppSettings["UseGoogleReverse"];

///<summary>
/// Initializes a new instance of the <see cref="LocationAPI"/> class.
///</summary>
public LocationAPI()
{

}

publicstaticstring 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();
}
}
}

转载于:https://www.cnblogs.com/lelese7en/archive/2011/06/07/2074124.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值