Figure out time by latitude/longitude?

前端 未结 5 468
别跟我提以往
别跟我提以往 2020-12-10 00:13

Here\'s the deal. I have a latitude/longitude set of a location. I need to figure out what the current time is in that location. Here\'s how I was getting it before:

相关标签:
5条回答
  • 2020-12-10 00:38

    Use this data dump and import it into your program. Then locate the country in which your lat/long exists and it's time zone. You can then calculate what time it is at a specific lat/lon.

    Geonames

    0 讨论(0)
  • 2020-12-10 00:45

    Unfortunately timezones (and time in general) is never as simple as you would like.

    For a simple approximation you can follow jer's suggestion. Longitude ranges from -180 to 180 degrees, there are 24 hours in a day, so you get 15 degrees of longitude per time zone. Center those time zones on 0 degrees longitude so UTC extends from -7.5 to 7.5, UTC+1 is from 7.5 to 22.5, UTC-1 is from -7.5 to -22.5, and so on. You would then have a very simplistic, and wrong, model of how we use time zones.

    Take a look at this map of time zones.

    • Time zones are not well ordered; regions in UTC-1 are adjacent to regions in UTC-3.
    • UTC-9.5, UTC-4.5, UTC+3.5, UTC+5.75, and UTC+13 are all valid time zones and actively in use.

    Once you get that sorted out then you can start to consider daylight savings time.

    0 讨论(0)
  • 2020-12-10 00:45

    No, you will have to write one. Since the latitude and longitude points are well known, and timezones are also well known (though change periodically), all you have to do is map where the lat/long boxes are in given timezones, and calculate if your current position is in which one of those boxes. Once you know which box you're in, you'll have enough info to figure out what your time is.

    0 讨论(0)
  • 2020-12-10 00:45

    This is not a trivial problem -- but it's not one without a solution. Services like Geonames and WeatherBug are very restrictive and/or costly so don't start developing something before you thoroughly understand their terms of service.

    Here are the steps that I followed for my Appcelerator-based iPhone app:

    1. Locate a list of coordinates that describe all timezones as polygons and read them into a SQLite table.
    2. Make a best guess of which timezone the user is in (or is seeking), based on the longitude. This will be several zones.
    3. Pull the polygons of each of the best-guess timezones from the database and run each one through a find-point-in-polygon algorithm. The one that returns true is the correct timezone.

    If you a not concerned about Daylight Saving Time, you're done, otherwise, you have a mess to deal with. The landscape of DST is completely illogical and changes frequently. The best I could do was to Google "Daylight saving time rules" (start looking here: Sources for Time Zone and Daylight Saving Time Data) and be prepared to do a lot of work setting up list that you can use for the calculation. Mine still does not work perfectly after a lot of tweaking.

    0 讨论(0)
  • 2020-12-10 00:46

    I've written a small Java class to do this, with the data embedded in the code. It could be easily translated to ObjectiveC. The database is embedded in the code itself. It's accurate to 22km.

    https://sites.google.com/a/edval.biz/www/mapping-lat-lng-s-to-timezones

    0 讨论(0)
提交回复
热议问题