问题
I need to display date and time based on the given lat/long in the app. I worked with Google API to get the timezone ID, but it should work offline too. Is there any way to get timezone without being connected to the internet?
回答1:
Try this one :
CLLocation *location = [[CLLocation alloc] initWithLatitude:50.449846
longitude:30.523629];
NSTimeZone *timeZone = [[APTimeZones sharedInstance] timeZoneWithLocation:location];
NSLog(@"%@", timeZone);
Find Library Here : https://github.com/Alterplay/APTimeZones
You can also do it by code (answered by 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);
}
来源:https://stackoverflow.com/questions/30528529/how-to-get-timezone-by-latitude-and-longitude