Objective c 如何在iOS中从经度和纬度识别时区

Objective c 如何在iOS中从经度和纬度识别时区,objective-c,ios5,latitude-longitude,nstimezone,Objective C,Ios5,Latitude Longitude,Nstimezone,如何找出给定经度和纬度所属的时区?以下是对我有效的技巧。可以从中提取时区标识符,并且您可以将此id用于时区 CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude]; [geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

如何找出给定经度和纬度所属的时区?

以下是对我有效的技巧。可以从中提取时区标识符,并且您可以将此id用于时区

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

    }];

我尝试了
APTimeZones
library。在我的例子中,我需要一个特定城市的时区和国家名称。 我浏览了图书馆,想知道它是如何运作的。它实际上有一个JSON格式的文件,其中包含所有时区及其对应的lat长度。它将lat long作为输入,并通过这个JSON文件循环,比较所有时区的lat long与输入lat long的距离。它返回距离输入lat long最短的时区

但问题是,对于一个大国边界上的城市来说,它返回给我邻国的时区,因为我也从中提取了国家代码,我得到了邻国

因此,在这种情况下,苹果的原生框架是非常好的

下面的代码对我很有效

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

}];
在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)
     }
}

是的,这是苹果的私有框架,但如果你从本机框架获得它,那么最好不要使用第三方库。在iOS更新中出现这种情况的可能性很可能是iOS 11,macOS10.13它是公共的。它显示用户的时间和时区,而不是位置。iOS 13.4这是没有第三方库的最优雅的解决方案!我不敢相信在CLPlacemark上有一个时区的地产:)是的,伙计,它值一份,所以我就这么做了。。快乐编码:)由于ios11、macOS10.13是公共框架,所以不需要考虑其他解决方案