如何在iOS中从经度和纬度中识别时区 [英] How to identify timezone from longitude and latitude in iOS

查看:158
本文介绍了如何在iOS中从经度和纬度中识别时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何找出给定经度和纬度落入哪个NSTimeZone?

How can I find out which NSTimeZone a given longitude and latitude fall in?

推荐答案

我试过 APTimeZones 库。在我的情况下,我需要Timezone以及来自特定城市的长途的国家名称。
我通过图书馆知道它是如何工作的。它实际上有一个JSON格式的文件,它包含所有时区以及它们对应的lat long。它将lat作为输入并循环通过此JSON文件,比较所有时区的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.

以下代码对我有用。

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);

}];

这篇关于如何在iOS中从经度和纬度中识别时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆