How to identify timezone from longitude and latitude in iOS

后端 未结 2 978
既然无缘
既然无缘 2020-12-06 17:29

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

2条回答
  •  不知归路
    2020-12-06 18:29

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

提交回复
热议问题