【问题标题】:How to get timezone by latitude and longitude [duplicate]如何通过纬度和经度获取时区[重复]
【发布时间】:2015-08-12 06:11:46
【问题描述】:

我需要根据应用中给定的纬度/经度显示日期和时间。我使用 Google API 来获取时区 ID,但它也应该离线工作。有什么方法可以在不连接到互联网的情况下获取时区?

【问题讨论】:

标签: ios geolocation timezone


【解决方案1】:

试试这个:

CLLocation *location = [[CLLocation alloc] initWithLatitude:50.449846
                                                  longitude:30.523629];

NSTimeZone *timeZone = [[APTimeZones sharedInstance] timeZoneWithLocation:location];
NSLog(@"%@", timeZone);

在此处查找图书馆:https://github.com/Alterplay/APTimeZones


也可以通过代码实现(https://stackoverflow.com/a/27054583/3202193回复):

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

[geoCoder reverseGeocodeLocation:currentLocaiton 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); 

    }

【讨论】:

  • +1 用于 APTimeZones 库,它具有离线时区数据库并可以使用它。但在某些情况下可能不准确,请查看项目的问题选项卡。夏令时时区更改和其他情况可能会出现问题
  • APTimeZones 进行最近点查找,这通常会出错。看他们的issue #2this answer 的部分有两个正方形的图。
猜你喜欢
  • 2011-02-04
  • 1970-01-01
  • 1970-01-01
  • 2013-03-23
  • 2014-10-10
  • 2012-07-30
  • 2016-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多