如何通过经纬度获取时区 [英] How to get timezone by latitude and longitude

查看:899
本文介绍了如何通过经纬度获取时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据应用中给定的经纬度显示日期和时间。我使用Google API来获取时区ID,但它也应该脱机工作。有没有办法获得时区而无需连接到互联网? 试试这个:

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

}


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?

解决方案

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

    }

这篇关于如何通过经纬度获取时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆