如何根据 RAILS 中的时区验证 GPS 坐标
我让用户在他们的偏好中设置时区,但我们也尝试在每次交互时从浏览器获取 GPS 坐标。如果 GPS 坐标落在特定时区范围内,我想警告用户更改时区。
可以采用什么方法呢?
我目前正在使用 Rails 3.1
I have my users set their timezone in their preferences but we also attempt to grab a GPS coordinate from the browser on each interaction. I would like to warn the user to change their timezone if the GPS coordinate fall within the specific timezone.
What methods can be used?
I'm currently using Rails 3.1
您可以将纬度除以 15°,然后四舍五入到最接近的整数,以获得以小时为单位的“自然”时区偏移量。但这只会对您在公海上有帮助,因为在陆地上,时区边界与数学上的理想边界有很大不同。在某些地方,例如中国西部,官方时间可能与地理标准时间相差多达 3 小时(甚至在夏令时调整之前)。
因此,除非您嵌入详细的时区边界地图(该地图会很大,并且需要某种更新机制,因为地方和国家政府决定绕时区移动),否则您希望做的最好的事情就是可靠地检测您的用户乘坐洲际航班但忘记更改时区。除非您有非常特定的受众,否则可能不值得这么麻烦。
You can divide the latitude by 15° and then round to the nearest integer to get the "natural" timezone offset in hours. But that will only help you on the high seas, because on land the timezone boundaries differ significantly from the mathematically ideal ones. In some places, such as western China, the official time can differ from the geographically standard one by as much as 3 hours (which is even before adjusting for daylight saving).
So unless you embed a detailed map of timezone boundaries (which would be large and require some kind of updating mechanism as local and national governments decide to move around the timezones), the best you could hope to do would be to detect reliably is if your user flies intercontinentally but forgets to change his timezone. Unless you have a very specific audience, that is probably not worth the trouble.