Calculating the Distance Between CitiesBy Mike Shaffer
Have you ever been to a company's website where you can key in your zip code and find which of their stores are closest to you? Or perhaps you've seen an employment website where you can narrow your search to return only those jobs which are within a 20 mile radius of your home. Have you ever wondered how that works, and how you could add that type of functionality to your website?
This article will show you how to incorporate this type of geographical processing into your website.
Do you remember sitting in high-school, taking your first trigonometry course, wondering to yourself, "When will this crap ever apply to me in the 'Real World'?" Well, my friend, today is that day. No, I am not going to make you dig out your old trig textbooks... nor am I going to go into the theory behind how these routines work. After all, these articles are meant to provide quick solutions to real-world problems, not to torture you with the arcana of your teen years.
So let's just say that these functions are derived from spherical trigonometry, and leave it at that. "But wait," I can hear some of my geek friends starting to say, "the planet we live on is more of an oblate spheroid, not a perfect sphere." To which I would say, "That is correct." I would then probably also add, under my breath, "Get a life," but that is a topic for another article entirely. These routines should be plenty accurate for the type of applications I mentioned above. In general, you can expect that the distance you receive will average an accuracy of +/- one percent.
The code below includes three routines. The main one, called
GetDistance, calculates the
distance between two points on the globe. To use it, you must pass the latitude and
longitude (in decimal degrees, e.g. 32.9697) of both points. You also pass the unit of
measure that you'd like the results returned in, either 'K' for kilometers, 'M' for statute
miles, or 'N' for nautical miles.
Here is an example of the output produced by the code below:
At this point, you may be wondering how to obtain information such as latitudes and longitudes for various points on the globe. There are several companies on the web who sell the complete US Zip code database, at prices ranging from $99 to several hundred dollars. My company, for example, sells the complete US database along with a high-speed component version of this article (which performs distance calculations in compiled code and also has many other features) for as little as $99.
DegToRads function is mainly used in support of the
routine, but can be used by itself. It simply converts decimal degrees to radians.
DecimalDegToDMS routine can be used to convert decimal degrees (e.g. 32.9697) to
a more readable degrees/minutes/seconds (e.g. 32° 58' 10").
So, using our example above, how would we go about creating a store finder application? It's actually pretty straightforward. You could ask a visitor to your website to give you their zip code. You would then look up their zip code in your handy-dandy zip code database (mentioned above). This would yield their latitude and longitude. You would then cycle through your database of store locations, calculating the distance from the user to every store, sorting them by distance. And hey, while they're waiting on this, why not tell them when the sun will be setting for their location this evening? :) When finished, present the user with the three stores that are closest to them.
Adding geographical calculations to you website can provide users with interesting and useful information. I hope this article has helped give you the headstart you need to implement such features. Feel free to contact me with any enhancements you make to this software!