How to identify timezone from longitude and latitude in iOS(如何在iOS中从经度和纬度识别时区)
问题描述
如何找出给定的经度和纬度属于哪个 NSTimeZone?
How can I find out which NSTimeZone a given longitude and latitude fall in?
推荐答案
我试过 APTimeZones
库.就我而言,我需要时区以及来自特定城市纬度的国家名称.我通过图书馆了解它是如何工作的.它实际上有一个 JSON 格式的文件,其中包含所有时区及其相应的经纬度.它将 lat long 作为输入并循环通过此 JSON 文件,比较所有时区的 lat long 与输入 lat long 的距离.它返回与输入 lat long 距离最短的时区.
I have tried APTimeZones
library. In my case I needed Timezone as well as country name from the lat long of a particular city.
I went through the library to know how it works. It actually has a file in JSON format which has all the timezones along with their corresponding lat longs. it takes the lat long as input and loops through this JSON file comparing the distances of all the timezone's lat long from the input lat long. It returns the time zone which has shortest distance from the input lat long.
但问题是对于一个大国边境的城市,它返回给我邻国的时区,因为我也从中提取了国家代码,所以我得到了邻国.
But the problem was for a city in the border of a big country, it returned me the timezone of neighbouring country, as I also extracted the country code from it, I got the neighbouring country.
所以在这种情况下,Apple 的原生框架非常好.
So Apple's native framework is far good in this case.
下面的代码对我很有效.
And the below code worked for me well.
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)
}
}
这篇关于如何在iOS中从经度和纬度识别时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在iOS中从经度和纬度识别时区


- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01