![]() |
|
|
Published: Saturday, January 08, 2000 By Mike Shaffer In my last article, Calculating the Distance Between Cities, I showed you how to calculate the distance between two geographical points on the Earth. In it, I jokingly mentioned that while you were scanning your database and performing all of the calculations for distance, you might want to entertain (or distract) the user by letting her know when the sun was going to set in her location (since by then you had her latitude and longitude).
Well, it's just my luck that people took me seriously and began sending emails asking how to perform sunrise and sunset calculations! :) So, here is a snippet of code that will calculate either sunrise or sunset for a given latitude and longitude. There are all kinds of caveats attached to this code, and to avoid any misunderstandings, please read them carefully:
Whew! Having now examined all of the disclaimers, let's look at the parameters required to
call the
Here is some sample code that shows how to call the
The result returned from the Yes, there are alternatives to using this code. One thing that I have seen many sites do is to perform a 'screen-scrape' from another site that already performs these calculations, such as www.weather.com. While that technique may work in general, it relies on the fact that weather.com is always up when you need it. When it comes to solving programming problems, I believe that 'elegant and reliable' solutions are much better than 'quick-and-dirty' solutions. (As a side note, you can learn how to grab information off of other web servers by reading, Grabbing Information From Other Servers.) It should be noted that this code is based on the work of several other people, most of whom specialize in the field of celestial mechanics. While it's true that I am an avid amateur astronomer, I cannot claim credit for developing the methods that are encapsulated by these routines. Please see the comments in the routines for the names of the people whose shoulders I was fortunate to be able to stand upon. For information on how to obtain a database of cities, zip codes, and longitude/latitude measurements, be sure to consult my previous article, or visit GreatData.com. Happy Programming!
Attachments:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||