問題描述
如何找出給定的經度和緯度屬于哪個 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中從經度和緯度識別時區的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!