Get Latitude and Longitude from Time Zone in Java
I'm trying to write a world clock app that returns the 开发者_JAVA百科sunrise and sunset information for a given time zone. The thinking is that the user will either specify the GMT offset or give the time zone explicitly. Then the application will look up the latitude and longitude using the java.util.TimeZone found (or possibly given the GMT offset). I realize the time zones are usually not a very accurate way to get geolocation information since they are mostly based on political borders, but it seems like something like this exists. Most of the sites/apis I've find will give the timezone for the latitude and longitude, but I want to do a reverse search (return latitude and longitude for the given time zone). Thanks in advance for your help.
Let me show you the level of error you can expect if you do this to my home country, New Zealand, which is in one time zone (excepting remote islands), yet stretches over a large range of latitudes:
Invercargill, New Zealand
Time zone: NZST
Sunrise today: 7:05 am
Auckland, New Zealand
Time zone: NZST
Sunrise today: 6:34 am
Expect similar problems with China (the whole country is one timezone), Chile, Argentina, and others.
Nice idea, but probably unworkable.
Geonames.org has a downloadable database of towns that includes all kinds of information, including timezone and population. You could use it to populate a database that maps from timezone to the location of the largest city in that timezone.
Note that GMT offsets are very useless for this purpose, but fortunately the continent/city terminology of timezone IDs makes it very easy for users to choose the correct one.
Time zone is an area, so you would have to return coordinates of the border of this area (and probably add some method to check given point is inside). So you have to starts with geo data. Keep in mind that lot of countries have summer time so the time zones are not constant.
After reading everyone's answer, I think I'll probably use Google's Geocoding API, which returns the latitude/longitude and timezone for a given physical address. I'd just have to add an interface that lets the user enter an address, city or post code in order to retrieve the info from the api and calculate the sunrise/sunset times. This is better than trying to store all this information locally in a database (which would be huge). Thanks to all who answered.
精彩评论