How to identify timezone from longitude and latitude in iOS
Asked Answered
D

5

10

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

Dishwater answered 8/2, 2012 at 6:43 Comment(0)
L
12

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)
     }
}
Lurlenelurline answered 20/6, 2016 at 6:10 Comment(2)
This is the most elegant solution without a 3rd party library! I can't believe there's a timeZone property on a CLPlacemark :)Shift
since ios11, macOS10.13 it is public framework, no need to look at other solutionsAcumen
A
2

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); 

    }];
Aswan answered 21/11, 2014 at 5:17 Comment(4)
Yes this is apple's private framework, but if you get it from native framework then it is best rather than using third party library.Aswan
The chances of this breaking in an iOS update is very likelyArgument
since ios11, macOS10.13 it is publicAcumen
This displays the user's time and timezone, not that of the location. iOS 13.4Coccyx
A
1

I have ported the DarkSky's timezone code to Swift, it uses accurate timezone shapes to generate a simplified database of timezones allowing for fast lookup and small size. APTimeZones is lacking as it's only looking for timezone which has the closest centre.

https://github.com/bialylis/tz-lookup-oss

Alkylation answered 9/1, 2023 at 18:38 Comment(0)
S
1

I created an another library to resolve coordinates to timezones and countries. https://github.com/patrick-zippenfenig/SwiftTimeZoneLookup

The timezone database is embedded as a bundle (~5 MB). I just updated it the latest release 2023b from timezone-boundary-builder

Usage:

import SwiftTimeZoneLookup


let database = try SwiftTimeZoneLookup()
guard let timezone = database.simple(latitude: 47.5, longitude: 8.6) else {
  fatalError("Timezone not found, coordinates invalid?")
}
print(timezone) // "Europe/Zurich"


guard let lookup = database.lookup(latitude: 47.5, longitude: 8.6) else {
  fatalError("Timezone not found, coordinates invalid?")
}
print(lookup) // SwiftTimeZoneLookupResult(timezone: "Europe/Zurich", countryName: Optional("Switzerland"), countryAlpha2: Optional("CH"))
Shelves answered 26/3, 2023 at 13:19 Comment(0)
V
-1

You can use Core Location to do it in Swift, however, it requires the user to be online and runs asynchronously.

import CoreLocation

func timeZone(forLatitude latitude: CLLocationDegrees, longitude: CLLocationDegrees, completion: @escaping (TimeZone?) -> Void) {
    let location = CLLocation(latitude: latitude, longitude: longitude)
    let geocoder = CLGeocoder()

    geocoder.reverseGeocodeLocation(location) { placemarks, error in
        if let error = error {
            print("Error reverse geocoding location: \(error.localizedDescription)")
            completion(nil)
        } else if let placemark = placemarks?.first {
            completion(placemark.timeZone)
        } else {
            print("No placemark data found.")
            completion(nil)
        }
    }
}
Visceral answered 19/7, 2023 at 20:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.