algorithm for getting time zone from geo coordinat

2019-02-09 03:38发布

问题:

I want to write app where user can point any place on map (not only cities) and get timezone in that place.

What data structure (app will not have Internet connectivity) and algorithm should I use? Where I can obtain needed data (I wont more accuracy then divining map into 24 rectangles)?

I will write my app in Java ME.

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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.