sus*_*use 10 objective-c latitude-longitude nstimezone ios5
如何找出给定经度和纬度落入哪个NSTimeZone?
我试过APTimeZones库.在我的情况下,我需要时区以及来自特定城市的纬度的国家名称.我通过图书馆了解它是如何工作的.它实际上有一个JSON格式的文件,它包含所有时区以及相应的lat long.它将lat作为输入并循环通过此JSON文件,比较所有时区的lat long与输入lat long的距离.它返回与输入lat long的距离最短的时区.
但问题是在一个大国边境的一个城市,它让我回到邻国的时区,因为我也从中提取了国家代码,我得到了邻国.
因此,Apple的本机框架在这种情况下非常好.
以下代码对我很有用.
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);
}];
Run Code Online (Sandbox Code Playgroud)
在斯威夫特
let location = CLLocation(latitude: your_latitude, longitude: your_longitude)
let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(location) { (placemarks, err) in
if let placemark = placemarks?[0] {
print(placemark.timeZone)
print(placemark.country)
}
}
Run Code Online (Sandbox Code Playgroud)
这是对我有用的技巧。可以从中提取时区标识符,并且您可以将此 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);
}];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3921 次 |
| 最近记录: |