algorithm for getting time zone from geo coordinates

試著忘記壹切 提交于 2019-11-30 12:03:49

Given that time zones are based on political entities rather than simply a physical lat/lon computation, I would create a data structure that mapped polygons over lat/lon coordinates into political entities (country and province/state) and then have a separate structure that mapped political entities and current date into timezone offset.

That way you not only avoid redundancy, but also:

  1. You can display DST reference information independently of a specific set of coordinates, and
  2. When some country changes the rules for when daylight saving time begins and ends, you have one place to make the update.

However, given the highly irregular shape of some borders, you'll need a fairly large data structure for accuracy, depending on the resolution of your input and/or display.

There are a number of web services that can do this for you (for example GeoNames has a great API). But if you don't have an Internet connection, then that's not something you're going to find directly in Java ME's standard libraries.

You could do something close, though: Store the coordinates of the cities corresponding to each time zone, then do a Voronoi tessellation so that you have the areas which are closest to each city. Then, when your users click on a particular geographic area, you simply map that point to the right section of the tessellation, and presto -- you've got the nearest city, which in turn determines the right time zone.

More sophisticated approaches are possible, but they also require significantly larger memory structures, which I assume is a constraint if you are running Java ME. This is a good compromise between space and speed.

Joel Neely's answer is good, but be aware this is a really tricky problem for political reasons. So for disputed areas like Kashmir or Tibet you could offend people by the decision you make.

Also, if you want to then use timezone information to compute time changes it gets even trickier, as decisions about whether Daylight saving time is used, and the date it changes can change with only 2 weeks notice. See: http://www.timeanddate.com/news/time/argentina-dst-2009-2010.html

The polygon information can be bought at http://www.worldtimeserver.com/time_zone_guide/ if you are interested. Disclaimer - I haven't bought this information, so don't know how good it is.

Well, if accuracy is not a requirement, why bother with a data structure ? Write a function which, given a longitude, returns the offset, expressed in hours, from the Greenwich meridian.

And if this doesn't work for you, I'd go with Joel Neely's answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!