2012-02-08 80 views

回答

0

这是我工作的诀窍。您可以从中提取时区标识符,并且可以将此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); 

    }]; 
+1

FYI这是一个苹果专用框架 – thedeveloper3124 2015-07-01 18:38:04

+0

是的,这是苹果公司的私人框架,但如果你从本地框架得到它的话,就最好,而不是使用第三方库。 – 2015-07-02 05:37:21

+0

这打破在iOS的更新的机会很可能 – thedeveloper3124 2015-08-18 18:44:33

2

我已经使用APTimeZones库。在我的情况下,我需要时区以及一个特定城市经纬度的国名。 我通过图书馆了解它是如何工作的。它实际上有一个JSON格式的数据库,其中包含所有时区以及相应的经纬度。它将经纬度作为输入,并在其数据库中循环比较所有时区的经纬度与输入纬度经度的距离。它返回距离输入纬度最短距离的时区。

但问题是在一个大国的边境城市,它返回我周边国家的时区,我也从中提取的国家代码,我得到了邻国。

所以苹果自带的框架是在这种情况下,远好。

而下面的代码工作对我来说很好。

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

}]; 
+0

这是没有第三方库中最完美的解决方案!我不相信有一个CLPlacemark时区物业:) – PostCodeism 2017-11-14 02:04:11

+0

是纨绔子弟,这是值得一份额,所以我做了..快乐编码:) – Roohul 2017-11-14 12:35:13