- 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
{
const string GOOGLE_API = "https://maps.googleapis.com";
const string GOOGLE_TIMEZONE_REQUEST =
"maps/api/timezone/xml";
const double LATITUDE = 30.4333;
const double LONGITUDE = -87.2000;
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 ( );
}
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 );
}
}
}
Hope that helps.