如何找出给定的经度和纬度属于哪个 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中从经度和纬度识别时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!