<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>effengud software &#187; latitude</title>
	<atom:link href="http://www.effengud.com/index.php/tag/latitude/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.effengud.com</link>
	<description>It&#039;s Simple. It&#039;s Elegant. It&#039;s effengud!™</description>
	<lastBuildDate>Tue, 10 Aug 2010 16:14:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Improving the &#8220;Calculating the Distance Between Cities&#8221; Algorithm</title>
		<link>http://www.effengud.com/index.php/2009/08/04/improving-the-calculating-the-distance-between-cities-algorithm/</link>
		<comments>http://www.effengud.com/index.php/2009/08/04/improving-the-calculating-the-distance-between-cities-algorithm/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 12:29:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[distance calculation]]></category>
		<category><![CDATA[latitude]]></category>
		<category><![CDATA[longitude]]></category>
		<category><![CDATA[ZIP code]]></category>

		<guid isPermaLink="false">http://www.effengud.com/wp/?p=61</guid>
		<description><![CDATA[This article serves as an addendum to my previous article entitled "<A HREF="http://www.effengud.com/index.php/2009/08/04/calculating-the-distance-between-cities/">Calculating the Distance Between Cities</A>". That article talked about finding the distance between cities as being a useful way to determine, for example, which of a company's many locations might be closest to a particular customer. ]]></description>
			<content:encoded><![CDATA[<p><P><br />
In that example, your store database may consist of several dozen to several hundred records (one for each store), and for each record you would have to perform the distance calculation. This is not so bad. But what if you had over 1,000 locations? Or what if you wanted to find all cities located within a particular radius from a customer, regardless of whether or not you have a store there?</p>
<p><P>In this case, you would certainly <b>NOT</b> want to loop through all cities within your database making the distance calculation. That would take a <b>VERY</b> long time. You might, in a flash of inspiration, attempt to perform the distance calculation only for cities within that customer&#8217;s state&#8230; but that would not work very well for people who live near a state&#8217;s border, or for people who live, say, in Rhode Island.</p>
<p><P>This article details a method for dramatically reducing the amount of processing necessary to find all locations within a certain radius.</p>
<p><P>By first performing a <i>rough trim</i> of the cities list based on latitude and longitude, we limit the number of calculations we must make. By using the algorithm below, we can accomplish that, as well as ensure that we are grabbing all cities within a particular area, regardless of state boundaries, etc.</p>
<p><P>Given a customer&#8217;s latitude (<CODE>iStartLat</CODE>) and longitude (<CODE>iStartLong</CODE>), and the radius we wish to search (<CODE>iRadius</CODE>, in miles), the following algorithm will return the latitude and longitude range for cites we will evaluate (using the formula provided in the last article). <B>NOTE</B>: Be aware that &#8212; as in the last article &#8212; this algorithm is accurate enough for most e-commerce applications, but <b>NOT</b> for navigational applications:</p>
<p><P><TABLE WIDTH=95% BORDER=0><br />
<TR><TD WIDTH=100% BGCOLOR=#CCCCCC><br />
<CODE><PRE><br />
&#8216; THIS VARIABLE SETS THE RADIUS IN MILES<br />
iRadius = 150</p>
<p>LatRange = iradius / ((6076 / 5280) * 60)<br />
LongRange = iRadius / (((cos(cdbl(iStartLat * _<br />
            3.141592653589 / 180)) * 6076.) / 5280.) * 60)</p>
<p>LowLatitude = istartlat &#8211; LatRange<br />
HighLatitude = istartlat + LatRange<br />
LowLongitude = istartlong &#8211; LongRange<br />
HighLongitude = istartlong + LongRange<br />
</PRE></CODE><br />
</TD></TR><br />
</TABLE></p>
<p><P>Now you can create a SQL statement which limits the recordset of cities in this manner:</p>
<p><P><TABLE WIDTH=95% BORDER=0><br />
<TR><TD WIDTH=100% BGCOLOR=#CCCCCC><br />
<CODE><PRE><br />
 SELECT *<br />
 FROM Locations<br />
 WHERE Latitude <= [HighLatitude]<br />
   AND Latitude >= [LowLatitude]<br />
   AND Longitude >= [LowLongitude]<br />
   AND Longitude <= [HighLongitude]<br />
</PRE></CODE><br />
</TD></TR></p>
<p></TABLE></p>
<p><P><I>(Note: This algorithm works pretty well for US and Canadian cities. Adapting the algorithm to other regions may require changing the comparison order due to negative latitudes, etc.)</I></p>
<p><P>Performing the distance calculation (covered in the <A HREF="http://www.effengud.com/index.php/2009/08/04/calculating-the-distance-between-cities/">previous article</A>) on the resulting recordset ensure that you are not wasting too many CPU cycles on cities that are obviously outside the radius you have specified.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.effengud.com/index.php/2009/08/04/improving-the-calculating-the-distance-between-cities-algorithm/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Calculating the Distance Between Cities</title>
		<link>http://www.effengud.com/index.php/2009/08/04/calculating-the-distance-between-cities/</link>
		<comments>http://www.effengud.com/index.php/2009/08/04/calculating-the-distance-between-cities/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 12:21:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[distance calculation]]></category>
		<category><![CDATA[latitude]]></category>
		<category><![CDATA[longitude]]></category>
		<category><![CDATA[ZIP code]]></category>

		<guid isPermaLink="false">http://www.effengud.com/wp/?p=57</guid>
		<description><![CDATA[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.
]]></description>
			<content:encoded><![CDATA[<p><P>Do you remember sitting in high-school, taking your first trigonometry course, wondering to yourself, &#8220;When will this crap ever apply to me in the &#8216;Real World&#8217;?&#8221;  Well, my friend, today is that day. No, I am not going to make you dig out your old trig textbooks&#8230; 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.</p>
<p><P>So let&#8217;s just say that these functions are derived from spherical trigonometry, and leave it at that. &#8220;But wait,&#8221; I can hear some of my geek friends starting to say, &#8220;the planet we live on is more of an oblate spheroid, not a perfect sphere.&#8221; To which I would say, &#8220;That is correct.&#8221; I would then probably also add, under my breath, &#8220;Get a life,&#8221; 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.</p>
<p><P>The code below includes three routines. The main one, called <CODE>GetDistance</CODE>, 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&#8217;d like the results returned in, either &#8216;K&#8217; for kilometers, &#8216;M&#8217; for statute miles, or &#8216;N&#8217; for nautical miles.</p>
<p><P>Here is an example of the output produced by the code below:</p>
<p><P><TABLE WIDTH=95% BORDER=0><br />
<TR><TD WIDTH=100% BGCOLOR=#CCCCCC><br />
<CODE><br />
	Calculating the distance between Dallas (75248) and San Antonio (78201)<P><br />
<P><br />
	262.513422981729 Miles<BR></p>
<p>	422.474402195107 Km<BR><br />
	228.117927751138 Nautical Miles<BR><br />
	Dallas&#8217; decimal latitude of 32.9697 can also be shown as 32° 58&#8242; 10&#8243;<BR><br />
</CODE><br />
</TD></TR></TABLE></p>
<p><P>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. <a href="http://www.effengud.com/zipper.asp">Click here</a> for more details.</p>
<p><P>The <CODE>DegToRads</CODE> function is mainly used in support of the <CODE>GetDistance</CODE> routine, but  can be used by itself. It simply converts decimal degrees to radians.</p>
<p><P>The <CODE>DecimalDegToDMS</CODE> routine can be used to convert decimal degrees (e.g. 32.9697) to a more readable degrees/minutes/seconds (e.g. 32° 58&#8242; 10&#8243;).</p>
<p><P><br />
So, using our example above, how would we go about creating a store finder application? It&#8217;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.</p>
<p><strong>NOTE:</strong> 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, <a href="http://www.effengud.com/index.php/2009/08/04/improving-the-calculating-the-distance-between-cities-algorithm/">click here</a>.</p>
<p><P>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 <script>document.write('<A HR'+'EF=\"mai'+'lto:mshaff'+'er@e'+'ffengud.com\"');</script>>contact me</A> with any enhancements you make to this software!</p>
<p>Article collateral materials:</p>
<p>&nbsp;&nbsp;&nbsp;<img src="/elements/effendot.gif" width="7" height="7">&nbsp;<a href="/articles/2points.html">View/download the script in text format</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.effengud.com/index.php/2009/08/04/calculating-the-distance-between-cities/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
