How to get timezone by latitude and longitude [duplicate]

孤街醉人 提交于 2019-12-23 01:46:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!