且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用C#中的纬度和经度获取时区和位置名称

更新时间:2023-08-30 15:56:43

访问这里......



如何找到的,基于位置的上纬度,和经度值 [ ^ ]
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



  • 希望有所帮助。