Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify timezone from longitude and latitude in iOS

How can I find out which NSTimeZone a given longitude and latitude fall in?

like image 875
suse Avatar asked Feb 08 '12 06:02

suse


People also ask

How do you calculate time zones using latitude and longitude?

We must start by finding the difference in longitude (or degrees) of the two places. We do this by adding the two numbers. Then, divide by the 15 degrees that occurs in one hour and this will give you the time difference between two locations through the International Date Line.

How do I see coordinates in IOS?

Get GPS Coordinates in Maps on iPhone and iPad Tap the current location button on the top right. When the blue circle for your spot appears on the map, tap it. Swipe up from the bottom to view full details for your location and you'll see the Latitude and Longitude.

Is there a longitude and latitude app?

Google Maps For Android users, Google Maps is the simplest way to either enter latitude and longitude coordinates or to find out your current coordinates. If you want to find your current coordinates, zoom into your location and hold down on the touchpad screen in the map.

How to find Lat and longitude coordinates on an iPhone?

If you touch the latitude and longitude coordinates on the Compass app display, iOS Maps will launch and show you that location. Part one solved. 2. Find LAT/LONG on a Map Google explains how to enter latitude and longitude coordinates into Google maps. On an iPhone (or iPad), it easiest to use the "Degrees and decimal minutes" format.

How to check time zone on iPhone or iPad?

This way, you can just right-swipe your iPhone or iPad lock screen (there’s no need to unlock your iPhone) any time you need to check time zones. First, download the free World Clock Time Widget app. Next, open the app and tap the plus sign (+) in the top-right corner. Search for a city, and then tap it to add it to the list.

How do I find my current location on an iPhone?

Apple's Compass app for iPhone. (Exact location obscured.) If you touch the latitude and longitude coordinates on the Compass app display, iOS Maps will launch and show you that location.

What's the best way to find latitude and longitude on Google Maps?

On an iPhone (or iPad), it easiest to use the "Degrees and decimal minutes" format. In the search field of Google maps, here's the format, for, say, north latitude and west longitude: For example, here's a location in southern Wyoming with simple integer degrees and minutes.


2 Answers

I have tried APTimeZones library. In my case I needed Timezone as well as country name from the lat long of a particular city. I went through the library to know how it works. It actually has a file in JSON format which has all the timezones along with their corresponding lat longs. it takes the lat long as input and loops through this JSON file comparing the distances of all the timezone's lat long from the input lat long. It returns the time zone which has shortest distance from the input lat long.

But the problem was for a city in the border of a big country, it returned me the timezone of neighbouring country, as I also extracted the country code from it, I got the neighbouring country.

So Apple's native framework is far good in this case.

And the below code worked for me well.

CLLocation *location = [[CLLocation alloc] initWithLatitude:your_latitude longitude:your_longitude];
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
[geoCoder reverseGeocodeLocation: location completionHandler:^(NSArray *placemarks, NSError *error)
{
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"Timezone -%@",placemark.timeZone);

//And to get country name simply.
NSLog(@"Country -%@",placemark.country);

}];

In Swift

let location = CLLocation(latitude: your_latitude, longitude: your_longitude)
let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(location) { (placemarks, err) in
     if let placemark = placemarks?[0] {
          print(placemark.timeZone)
          print(placemark.country)
     }
}
like image 144
Roohul Avatar answered Oct 15 '22 15:10

Roohul


Here is the trick that worked for me. From which timezone identifier can be extracted and you can use this id for timezone.

CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

[geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

    if (error == nil && [placemarks count] > 0) {

        placeMark = [placemarks lastObject];
         NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"identifier = \"[a-z]*\\/[a-z]*_*[a-z]*\"" options:NSRegularExpressionCaseInsensitive error:NULL];
        NSTextCheckingResult *newSearchString = [regex firstMatchInString:[placeMark description] options:0 range:NSMakeRange(0, [placeMark.description length])];
        NSString *substr = [placeMark.description substringWithRange:newSearchString.range];
        NSLog(@"timezone id %@",substr); 

    }];
like image 39
Dharmesh Vaghani Avatar answered Oct 15 '22 17:10

Dharmesh Vaghani