使用C#中的纬度和经度获取时区和位置名称 [英] get timezone and location name using latitude and longitude in C#
本文介绍了使用C#中的纬度和经度获取时区和位置名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用一些谷歌API但我不需要谷歌地图,因为我必须输入纬度和经度来获取位置名称和时区。我还使用了提供TimeZoneType的Microsoft Exchange webservice dll,它提供了不成功的gettimezonebycordinates(),因此我请求通过提供一些帮助我解决此问题的链接或建议来帮助我。
i have tried using some google api but i dont require google map, as i have to input the latitude and longitude to get the location name and timezone. i also used the Microsoft Exchange webservice dll that provides TimeZoneType which gives gettimezonebycordinates() which is not successful so i request to please help me in this regard by giving some links or suggestion that help me to work on this.
推荐答案
访问这里......
如何找到的,基于位置的上纬度,和经度值 [<一个href =http://www.dotnetspider.com/forum/325082-How-to-find-Location-based-on-Latitude-and-Longitude-values.aspx\"target =_ blanktitle =New Window > ^ ]
visit here...
How-to-find-Location-based-on-Latitude-and-Longitude-values[^]
- 如果您还没有NuGet,请下载它。
- 使用NuGet下载RestSharp dll。
- 将相应的RestSharp dll添加到项目引用中。
- 使用RestSharp添加; 代码的顶部。
- 在您的代码中,您正在使用JSON检索。这适用于Javascript,但在C#中它会带来困难。我建议使用XML检索。
- 我认为以下是你想要的:
- If you don't already have NuGet, download it.
- Use NuGet to download the RestSharp dll.
- Add the appropriate RestSharp dll to the project references.
- Add a using RestSharp; to the top of your code.
- In your code you are using JSON retrieval. This is fine for Javascript but in C# it raises difficulties. I'd suggest using the XML retrieval.
- I think the following does what you want:
using System;
using System.Xml;
using RestSharp;
namespace LocationTimeZone
{
// ************************************************* class Program
class Program
{
const string GOOGLE_API = "https://maps.googleapis.com";
const string GOOGLE_TIMEZONE_REQUEST =
"maps/api/timezone/xml";
// Pensacola, FL
const double LATITUDE = 30.4333;
const double LONGITUDE = -87.2000;
// ****************************************************** Main
static void Main ( string [ ] args )
{
string time_zone = String.Empty;
time_zone = get_time_zone ( LATITUDE,
LONGITUDE,
DateTime.Now );
Console.WriteLine ( "Time Zone: " + time_zone );
Console.Write ( "Press any key to exit" );
Console.ReadKey ( );
}
// ********************************************* get_time_zone
static string get_time_zone ( double latitude,
double longitude,
DateTime date )
{
RestClient client;
string location;
RestRequest request;
RestResponse response;
TimeSpan time_since_midnight_1970;
double time_stamp;
string time_zone = "In error";
try
{
client = new RestClient ( GOOGLE_API );
request = new RestRequest ( GOOGLE_TIMEZONE_REQUEST,
Method.GET );
location = String.Format ( "{0},{1}",
latitude,
longitude );
time_since_midnight_1970 = date.Subtract (
new DateTime ( 1970, 1, 1, 0, 0, 0 ) );
time_stamp = time_since_midnight_1970.TotalSeconds;
request.AddParameter ( "location", location );
request.AddParameter ( "timestamp", time_stamp );
request.AddParameter ( "sensor", "false" );
response = ( RestResponse ) client.Execute (
request );
if ( response.StatusDescription.Equals ( "OK" ) )
{
XmlNode node;
XmlDocument xml_document = new XmlDocument ( );
xml_document.LoadXml ( response.Content );
node = xml_document.SelectSingleNode (
"/TimeZoneResponse/time_zone_name" );
if ( node != null )
{
time_zone = node.InnerText;
}
}
}
catch ( Exception ex )
{
time_zone = "In error";
}
return ( time_zone );
}
} // class Program
} // namespace LocationTimeZone
这篇关于使用C#中的纬度和经度获取时区和位置名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文