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 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01