Sei sulla pagina 1di 8

5/9/2017 [Solved]gettimezoneandlocationnameusinglatitudeandlongitudeinC#CodeProject

12,921,950 members 53,121 online Sign in

Searchforarticles,questions,tips
articles Q&A forums lounge

Ask a Question All Unanswered FAQ

get timezone and location name using latitude and longitude in


C#
See more: C# C#4.0 Rate this:

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 11Apr14 3:33am


bunty swapnil 520

Add a Solution

Comments

RyanDev 11Apr14 9:57am


I believe Google apis will give you what you need. Where are you stuck?

bunty swapnil 11Apr14 10:03am


public DateTime GetLocalDateTimedouble latitude, double longitude, DateTime date
{
var client = new RestClient"https://maps.googleapis.com";
https://www.codeproject.com/Questions/758354/gettimezoneandlocationnameusinglatitudeand 1/8
5/9/2017 [Solved]gettimezoneandlocationnameusinglatitudeandlongitudeinC#CodeProject

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.AddSecondsresponse.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

RyanDev 11Apr14 10:05am


Did it not say where RestClient comes from wherever you found this code?

digimanus 11Apr14 13:24pm


might be meant as WebRequest?

2 solutions Top Rated Most Recent

Solution 2 Rate this:

visit here...

HowtofindLocationbasedonLatitudeandLongitudevalues[^]

Posted 11Apr14 23:39pm


Rakesh Meel 17.4K

Comments

bunty swapnil 12Apr14 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 12Apr14 12:13pm


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RestSharp;
using System.Xml;
using System.IO;
namespace LocationTimeZone
{

https://www.codeproject.com/Questions/758354/gettimezoneandlocationnameusinglatitudeand 2/8
5/9/2017 [Solved]gettimezoneandlocationnameusinglatitudeandlongitudeinC#CodeProject

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 Mainstring[] args


{
string time_zone = String.Empty;

time_zone = get_time_zoneLATITUDE,
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.FromSerializedStringtime_zone;// issue is here when debug says null in the timezone object
DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtctimeUtc, 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_zonedouble 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 RestClientGOOGLE_API;
request = new RestRequestGOOGLE_TIMEZONE_REQUEST,
Method.GET;
location = String.Format"{0},{1}",
latitude,
longitude;
time_since_midnight_1970 = date.Subtract
new DateTime1970, 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 = RestResponseclient.Execute
request;
if response.StatusDescription.Equals"OK"
https://www.codeproject.com/Questions/758354/gettimezoneandlocationnameusinglatitudeand 3/8
5/9/2017 [Solved]gettimezoneandlocationnameusinglatitudeandlongitudeinC#CodeProject

{
XmlNode node;
XmlDocument xml_document = new XmlDocument;

xml_document.LoadXmlresponse.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 12Apr14 16:14pm


I answered your location name from lat/long above.

Current date time is DateTime.Now.

bunty swapnil 13Apr14 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 13Apr14 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.

bunty swapnil 14Apr14 0:38am


Thanks a lot and i am getting the location name and Time with respect to UTC. so will convert it.
Thank you so much you rock!! :

Thanks all for guiding me.

Solution 1 Rate this:

https://www.codeproject.com/Questions/758354/gettimezoneandlocationnameusinglatitudeand 4/8
5/9/2017 [Solved]gettimezoneandlocationnameusinglatitudeandlongitudeinC#CodeProject

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:

Hide Expand Copy Code


usingSystem;
usingSystem.Xml;

usingRestSharp;

namespaceLocationTimeZone
{

//*************************************************classProgram

classProgram
{
conststringGOOGLE_API="https://maps.googleapis.com";
conststringGOOGLE_TIMEZONE_REQUEST=
"maps/api/timezone/xml";

//Pensacola,FL
constdoubleLATITUDE=30.4333;
constdoubleLONGITUDE=87.2000;

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

staticvoidMain(string[]args)
{
stringtime_zone=String.Empty;

time_zone=get_time_zone(LATITUDE,
LONGITUDE,
DateTime.Now);
Console.WriteLine("TimeZone:"+time_zone);

Hope that helps.

Posted 11Apr14 9:36am Updated 11Apr14 9:40am v2


gggustafson 57.4K

https://www.codeproject.com/Questions/758354/gettimezoneandlocationnameusinglatitudeand 5/8
5/9/2017 [Solved]gettimezoneandlocationnameusinglatitudeandlongitudeinC#CodeProject

Comments

bunty swapnil 12Apr14 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 12Apr14 9:47am


See Reverse Geocoding at https://developers.google.com/maps/documentation/geocoding.

bunty swapnil 12Apr14 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.

Add your solution here

Preview

Existing Members ...or Join us


Sign in to your account Download, Vote, Comment, Publish.

Your Email Your Email

Password Optional Password

Forgot your password?

https://www.codeproject.com/Questions/758354/gettimezoneandlocationnameusinglatitudeand 6/8
5/9/2017 [Solved]gettimezoneandlocationnameusinglatitudeandlongitudeinC#CodeProject

I have read and agree to the Terms of Service


and Privacy Policy
Please subscribe me to the CodeProject
newsletters

Submit your solution!

When answering a question please:

1. Read the question carefully.

2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.

3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults
are not welcome.

Let's work to help developers, not make them feel stupid.

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

Advertise | Privacy | Mobile Layout: fixed | fluid Copyright CodeProject, 19992017


Web01 | 2.8.170424.1 | Last Updated 12 Apr 2014 All Rights Reserved. Terms of Service

CodeProject, 503250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 4168498900 x 100

https://www.codeproject.com/Questions/758354/gettimezoneandlocationnameusinglatitudeand 7/8
5/9/2017 [Solved]gettimezoneandlocationnameusinglatitudeandlongitudeinC#CodeProject

https://www.codeproject.com/Questions/758354/gettimezoneandlocationnameusinglatitudeand 8/8

Potrebbero piacerti anche