Monday, August 13, 2012 effengud software RSS Feed

Calculating the Distance Between Cities

Calculating the Distance Between Cities

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:

Calculating the distance between Dallas (75248) and San Antonio (78201)

262.513422981729 Miles

422.474402195107 Km

228.117927751138 Nautical Miles

Dallas’ decimal latitude of 32.9697 can also be shown as 32° 58′ 10″

At this point, you may be wondering how to obtain information such as latitudes and longitudes for various points on the globe. effengud software sells a high-speed COM component and the complete US Zip code database for as little as $99. Click here for more details.

The DegToRads function is mainly used in support of the GetDistance routine, but can be used by itself. It simply converts decimal degrees to radians.

The 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. When finished, present the user with the three stores that are closest to them.

NOTE: This article has a follow-up article that explains how to greatly reduce the search time required for a dealer locater application. To read it, click here.

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!

Article collateral materials:

    View/download the script in text format

Tags: , , ,