我如何确定给定经度和纬度所处的NSTimeZone?
我已经尝试过APTimeZones
库。在我的案例中,我需要从特定城市的经纬度获取时区和国家名称。
我查看了该库以了解其工作原理。实际上,它有一个以JSON格式呈现的文件,其中包含所有时区及其对应的经纬度。它接受经纬度作为输入,并循环遍历此JSON文件,比较所有时区的经纬度与输入经纬度之间的距离。然后返回从输入经纬度到最短距离的时区。
但问题是,对于位于大国边界的城市,它会返回相邻国家的时区。由于我也从中提取了国家代码,因此我得到了相邻的国家。
所以,在这种情况下,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);
}];
在Swift中
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)
}
}
这里是我使用的技巧。可以从中提取时区标识符,并且您可以使用此标识符来设置时区。
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);
}];
我创建了另一个库,用于将坐标解析为时区和国家。https://github.com/patrick-zippenfenig/SwiftTimeZoneLookup
时区数据库作为一个bundle(约5 MB)嵌入其中。我刚刚更新了最新版本2023b from timezone-boundary-builder
使用方法:
import SwiftTimeZoneLookup
let database = try SwiftTimeZoneLookup()
guard let timezone = database.simple(latitude: 47.5, longitude: 8.6) else {
fatalError("Timezone not found, coordinates invalid?")
}
print(timezone) // "Europe/Zurich"
guard let lookup = database.lookup(latitude: 47.5, longitude: 8.6) else {
fatalError("Timezone not found, coordinates invalid?")
}
print(lookup) // SwiftTimeZoneLookupResult(timezone: "Europe/Zurich", countryName: Optional("Switzerland"), countryAlpha2: Optional("CH"))
APTimeZones
只能查找最接近中心的时区,功能不够完善。
import CoreLocation
func timeZone(forLatitude latitude: CLLocationDegrees, longitude: CLLocationDegrees, completion: @escaping (TimeZone?) -> Void) {
let location = CLLocation(latitude: latitude, longitude: longitude)
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location) { placemarks, error in
if let error = error {
print("Error reverse geocoding location: \(error.localizedDescription)")
completion(nil)
} else if let placemark = placemarks?.first {
completion(placemark.timeZone)
} else {
print("No placemark data found.")
completion(nil)
}
}
}