Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
4.09/5 (4 votes)
See more:
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.
Posted
Comments
ZurdoDev 11-Apr-14 9:57am    
I believe Google apis will give you what you need. Where are you stuck?
bunty swapnil 11-Apr-14 10:03am    
public DateTime GetLocalDateTime(double latitude, double longitude, DateTime date)
{
var client = new RestClient("https://maps.googleapis.com");
var request = new RestRequest("maps/api/timezone/json", Method.GET);
request.AddParameter("location", latitude + "," + longitude);
request.AddParameter("timestamp", date.ToTimestamp());
request.AddParameter("sensor", "false");
var response = client.Execute<googletimezone>(request);

return date.AddSeconds(response.Data.rawOffset + response.Data.dstOffset);
}

in code behind i have written this code and there is no reference or namespace for rest client class. i herd there is REstSharp library for .net in github but did not help me out to go further. mainly i need the timezone and location name based on latitude and longitude, but here if i use this code with some library do u think i will get what i have mentioned
ZurdoDev 11-Apr-14 10:05am    
Did it not say where RestClient comes from wherever you found this code?
Herman<T>.Instance 11-Apr-14 13:24pm    
might be meant as WebRequest?

 
Share this answer
 
Comments
bunty swapnil 12-Apr-14 8:29am    
i looked into the link thanks it was useful but my scenario is where i need to get the location name,date time from latitude and longitude using console c#.net. From the above code i am able to get the timezone how shall i use to convert to the current date time as well as location name.

Plz guide me.
bunty swapnil 12-Apr-14 12:13pm    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RestSharp;
using System.Xml;
using System.IO;
namespace LocationTimeZone
{
class Program
{
const string GOOGLE_API = "https://maps.googleapis.com";
const string GOOGLE_TIMEZONE_REQUEST =
"maps/api/timezone/xml";

// Pensacola, FL
const double LATITUDE = 27.172079;
const double LONGITUDE = 78.036618;

// ****************************************************** Main

static void Main(string[] args)
{
string time_zone = String.Empty;

time_zone = get_time_zone(LATITUDE,
LONGITUDE,
DateTime.Now);

//this will take timezone object i.e timezone id to give the current date time
DateTime timeUtc = DateTime.Now;
string s = time_zone;
TimeZoneInfo cstZone = TimeZoneInfo.FromSerializedString(time_zone);// issue is here when debug says null in the timezone object
DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone);
Console.WriteLine("Current date time of the given timezone -received from latitude and longitude" + cstZone);

Console.WriteLine("Timezone" + 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);
}


}
}


the issue with this not able to take timezone object that has timezone id to get current date time and also not able to get location name on basis of this.PLZ help me regarding this.
gggustafson 12-Apr-14 16:14pm    
I answered your location name from lat/long above.

Current date time is DateTime.Now.
bunty swapnil 13-Apr-14 4:45am    
thanks i got the location name by changes i made by passing timezone object from the code and passed into timzoneinfo.gettimezonebid() method.
I am getting in terms UTC+6:00, But i need date and time current format. tell me what to do plz
gggustafson 13-Apr-14 11:01am    
For date time formatting see Custom Date and Time Format Strings.

I am going to guess that the latitude and longitude you supplied to the Google API was somewhere in India. Thus the UTC+6. If by "current format" you mean some other time zone, then see Converting Times Between Time Zones.

Hope that helps.

  • 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:
  • C#
    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

  • Hope that helps.
 
Share this answer
 
v2
Comments
bunty swapnil 12-Apr-14 3:16am    
Thanks For Replying. @gggustafson, you are awesome and the code helped me a lot
Really you guys rock!. one more help can i use the same api to get the location name as well throught latitude and longitude.
gggustafson 12-Apr-14 9:47am    
See Reverse Geocoding at https://developers.google.com/maps/documentation/geocoding.
bunty swapnil 12-Apr-14 3:20am    
Thanks All for replying and apart form google api /using RestSharp interface for google api, is there any inbuild packages or classes in .net or use any Microsoft dll or something in .net where i can use for getting location name,Current Timezone using latitude and longitude.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900