特定の経度と緯度がどのNSTimeZoneに該当するかを確認するにはどうすればよいですか?
5047 次
2 に答える
11
私はライブラリを試し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);
}];
スイフトで
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)
}
}
于 2016-06-20T06:10:10.237 に答える
1
これが私のために働いたトリックです。どのタイムゾーン識別子から抽出でき、この 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);
}];
于 2014-11-21T05:17:48.560 に答える