<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.3" -->
<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/"
	>

<channel>
	<title>EatAtSporks</title>
	<link>http://www.eatatsporks.com</link>
	<description>Better than a poke in the eye</description>
	<pubDate>Thu, 20 Mar 2008 20:05:43 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
	<language>en</language>
			<item>
		<title>Calculating Distances with MySQL and Zip Codes</title>
		<link>http://www.eatatsporks.com/2008/03/20/calculating-distances-with-mysql-and-zip-codes/</link>
		<comments>http://www.eatatsporks.com/2008/03/20/calculating-distances-with-mysql-and-zip-codes/#comments</comments>
		<pubDate>Thu, 20 Mar 2008 19:47:27 +0000</pubDate>
		<dc:creator>Nobes</dc:creator>
		
		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[php mysql geocoding]]></category>

		<guid isPermaLink="false">http://www.eatatsporks.com/2008/03/20/calculating-distances-with-mysql-and-zip-codes/</guid>
		<description><![CDATA[I recently have been working on a major project where were setting up a system to search through candidates and find people within a certain distance from a specific zipcode and i wanted to put my ideas out there. I found a lot of help on the MySQL mailing list archives and other places across [...]]]></description>
			<content:encoded><![CDATA[<p>I recently have been working on a major project where were setting up a system to search through candidates and find people within a certain distance from a specific zipcode and i wanted to put my ideas out there. I found a lot of help on the MySQL mailing list archives and other places across the net, and because of that i wanted to share my solution with others who may run into these questions.</p>
<p>Ok so here&#8217;s my problem, given a zip code of say 27615, (which I find the latitude and longitude of through Yahoo), I wanted to find everyone who is within 50 miles of that location. I wanted most of the heavy lifting to be done in MySQL since its known for being fast for doing complex math calculations so that&#8217;s where I started, I also wanted it to be accurate, but the two don&#8217;t come easy. I found the best formula was to use the haversine formula, if you don&#8217;t know what this is, I suggest you head out to Google and do a little searching. Basically it can calculate the distance between two points using latitude and longitude and also account for the fact that the planet isn&#8217;t flat and it&#8217;s also not a perfect circle.</p>
<p>Here is a simple function using the formula in PHP format:</p>

<div class="wp_codebox"><table width="100%" ><tr><td colspan="2" class="msgheader"><div class="right"><a href="###" class="smalltxt" onclick="copycode($('3code8'));">[Copy to clipboard]</a><a class="smalltxt" href="###" onclick="toggle_collapse('38');">[<span id="38_symbol">-</span>]</a></div><div class="left"><span id="l3code8"><a href="#" onclick="javascript:showCodeTxt('3code8'); return false;">View Code</a>PHP</span></div></td></tr><tr id="38"><td class="code" id="3code8"><pre class="php"><span style="color: #000000; font-weight: bold;">function</span> calculateDistLatLong<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$lat1</span>, <span style="color: #0000ff;">$lon1</span>, <span style="color: #0000ff;">$lat2</span>, <span style="color: #0000ff;">$lon2</span>,  <span style="color: #0000ff;">$units</span> = <span style="color: #ff0000;">&quot;S&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #0000ff;">$a</span> = <span style="color: #000066;">pow</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">sin</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span> * <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$lat2</span> -  <span style="color: #0000ff;">$lat1</span><span style="color: #66cc66;">&#41;</span> / <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> ,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> +
        <span style="color: #000066;">cos</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span>  * <span style="color: #0000ff;">$lat1</span><span style="color: #66cc66;">&#41;</span> *
        <span style="color: #000066;">cos</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span>  * <span style="color: #0000ff;">$lat2</span><span style="color: #66cc66;">&#41;</span> *
        <span style="color: #000066;">pow</span><span style="color: #66cc66;">&#40;</span>  
            <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">sin</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span> * <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$lon2</span> - <span style="color: #0000ff;">$lon1</span><span style="color: #66cc66;">&#41;</span>/<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> 
     ,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$c</span> = <span style="color: #cc66cc;">2</span> * <span style="color: #000066;">atan2</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066;">sqrt</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$a</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #000066;">sqrt</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>-<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$a</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">//R (Earth  Radius) = 3956.0 mi = 3437.7 nm = 6367.0 km</span>
&nbsp;
<span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$units</span><span style="color: #66cc66;">&#41;</span>  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'S'</span>: <span style="color: #808080; font-style: italic;">// STATUTE MILES</span>
    <span style="color: #0000ff;">$R</span> = <span style="color: #cc66cc;">3956.0</span>;
    <span style="color: #b1b100;">break</span>;
<span style="color: #b1b100;">case</span>  <span style="color: #ff0000;">'N'</span>: <span style="color: #808080; font-style: italic;">// NAUTICAL</span>
    <span style="color: #0000ff;">$R</span>  = <span style="color: #cc66cc;">3437.7</span>;
    <span style="color: #b1b100;">break</span>;
<span style="color: #b1b100;">case</span>  <span style="color: #ff0000;">'K'</span>: <span style="color: #808080; font-style: italic;">// KILOMETERS</span>
    <span style="color: #0000ff;">$R</span>  = <span style="color: #cc66cc;">6367.0</span>;
    <span style="color: #b1b100;">break</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$R</span> * <span style="color: #0000ff;">$c</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>A modified MySQL version is this:</p>

<div class="wp_codebox"><table width="100%" ><tr><td colspan="2" class="msgheader"><div class="right"><a href="###" class="smalltxt" onclick="copycode($('3code9'));">[Copy to clipboard]</a><a class="smalltxt" href="###" onclick="toggle_collapse('39');">[<span id="39_symbol">-</span>]</a></div><div class="left"><span id="l3code9"><a href="#" onclick="javascript:showCodeTxt('3code9'); return false;">View Code</a>MYSQL</span></div></td></tr><tr id="39"><td class="code" id="3code9"><pre class="mysql"><span style="color: #cc66cc;">3956</span> * <span style="color: #cc66cc;">2</span> *
    <span style="color: #993333; font-weight: bold;">atan2</span><span style="color: #66cc66;">&#40;</span>
        <span style="color: #993333; font-weight: bold;">sqrt</span><span style="color: #66cc66;">&#40;</span>
            <span style="color: #993333; font-weight: bold;">pow</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>
                <span style="color: #993333; font-weight: bold;">sin</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span> * <span style="color: #66cc66;">&#40;</span>$lat1 - $lat2<span style="color: #66cc66;">&#41;</span>/<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#41;</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> +
        <span style="color: #993333; font-weight: bold;">cos</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span>  * $lat2<span style="color: #66cc66;">&#41;</span> *
        <span style="color: #993333; font-weight: bold;">cos</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span>  * $lat1<span style="color: #66cc66;">&#41;</span> *
        <span style="color: #993333; font-weight: bold;">pow</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>
            <span style="color: #993333; font-weight: bold;">sin</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span> * <span style="color: #66cc66;">&#40;</span>$long1 - $long2<span style="color: #66cc66;">&#41;</span> / <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#41;</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span> ,
    <span style="color: #993333; font-weight: bold;">sqrt</span><span style="color: #66cc66;">&#40;</span>
        <span style="color: #cc66cc;">1</span>  - <span style="color: #66cc66;">&#40;</span>
            <span style="color: #993333; font-weight: bold;">pow</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>
                <span style="color: #993333; font-weight: bold;">sin</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span>  * <span style="color: #66cc66;">&#40;</span>$lat1 - $lat2<span style="color: #66cc66;">&#41;</span> / <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#41;</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> +
            <span style="color: #993333; font-weight: bold;">cos</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span>  * $lat2<span style="color: #66cc66;">&#41;</span> *
            <span style="color: #993333; font-weight: bold;">cos</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span>  * $lat1<span style="color: #66cc66;">&#41;</span> *
            <span style="color: #993333; font-weight: bold;">pow</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>
                <span style="color: #993333; font-weight: bold;">sin</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span>  * <span style="color: #66cc66;">&#40;</span>$long1 - $long2<span style="color: #66cc66;">&#41;</span>/<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#41;</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> AS geoCodeDistance</pre></td></tr></table></div>

<p>As you can see, pretty complex. But mainly because of the math function.</p>
<p>My initial problem was that the SQL query itself was taking around 10 seconds to run against 30,000+ records, so what I initially did was to move the code outside of the query and process the distance with PHP. This brought the query time down to around 3 seconds and the total process time down to around 6 seconds. This was still unacceptable. So I figured out there was a way to cut down the number of results I needed to process within the same query. I found another similar math function to trim my results. I found the latitude and longitude of my starting coordinates then calculated out a rough square shaped perimeter of the radius I was searching.</p>
<p>When I was using the PHP method previously, I did this then ran the results through the function above to narrow down the results. If it returned a value greater than my $radius I would throw it out.<br />
So with the assumptions of $lat2 and $long2 being my source, or where I want to search from.</p>

<div class="wp_codebox"><table width="100%" ><tr><td colspan="2" class="msgheader"><div class="right"><a href="###" class="smalltxt" onclick="copycode($('3code10'));">[Copy to clipboard]</a><a class="smalltxt" href="###" onclick="toggle_collapse('310');">[<span id="310_symbol">-</span>]</a></div><div class="left"><span id="l3code10"><a href="#" onclick="javascript:showCodeTxt('3code10'); return false;">View Code</a>PHP</span></div></td></tr><tr id="310"><td class="code" id="3code10"><pre class="php"><span style="color: #0000ff;">$lat_range</span> = <span style="color: #0000ff;">$radius</span> / <span style="color: #cc66cc;">69.172</span>;
<span style="color: #0000ff;">$lon_range</span> = <span style="color: #000066;">abs</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$radius</span> / <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">cos</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$lat</span><span style="color: #66cc66;">&#41;</span> * <span style="color: #cc66cc;">69.172</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$min_lat</span> = <span style="color: #000066;">number_format</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$lat2</span> - <span style="color: #0000ff;">$lat_range</span>, <span style="color: #ff0000;">&quot;6&quot;</span>,  <span style="color: #ff0000;">&quot;.&quot;</span>, <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$max_lat</span> = <span style="color: #000066;">number_format</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$lat2</span> + <span style="color: #0000ff;">$lat_range</span>, <span style="color: #ff0000;">&quot;6&quot;</span>,  <span style="color: #ff0000;">&quot;.&quot;</span>, <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$min_lon</span> = <span style="color: #000066;">number_format</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$long2</span> - <span style="color: #0000ff;">$lon_range</span>, <span style="color: #ff0000;">&quot;6&quot;</span>,  <span style="color: #ff0000;">&quot;.&quot;</span>, <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$max_lon</span> = <span style="color: #000066;">number_format</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$long2</span> + <span style="color: #0000ff;">$lon_range</span>, <span style="color: #ff0000;">&quot;6&quot;</span>,  <span style="color: #ff0000;">&quot;.&quot;</span>, <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Then add this to the sql query</p>

<div class="wp_codebox"><table width="100%" ><tr><td colspan="2" class="msgheader"><div class="right"><a href="###" class="smalltxt" onclick="copycode($('3code11'));">[Copy to clipboard]</a><a class="smalltxt" href="###" onclick="toggle_collapse('311');">[<span id="311_symbol">-</span>]</a></div><div class="left"><span id="l3code11"><a href="#" onclick="javascript:showCodeTxt('3code11'); return false;">View Code</a>PHP</span></div></td></tr><tr id="311"><td class="code" id="3code11"><pre class="php"><span style="color: #0000ff;">$sql</span> .= <span style="color: #ff0000;">&quot; (geoLatitude BETWEEN '$min_lat' AND '$max_lat' AND  geoLongitude BETWEEN '$min_lon' AND '$max_lon')&quot;</span>;</pre></td></tr></table></div>

<p>What I eventually settled on was a mixture of the two. By running the first query in the select then placing</p>

<div class="wp_codebox"><table width="100%" ><tr><td colspan="2" class="msgheader"><div class="right"><a href="###" class="smalltxt" onclick="copycode($('3code12'));">[Copy to clipboard]</a><a class="smalltxt" href="###" onclick="toggle_collapse('312');">[<span id="312_symbol">-</span>]</a></div><div class="left"><span id="l3code12"><a href="#" onclick="javascript:showCodeTxt('3code12'); return false;">View Code</a>PHP</span></div></td></tr><tr id="312"><td class="code" id="3code12"><pre class="php"><span style="color: #0000ff;">$sql</span> .= <span style="color: #ff0000;">&quot; HAVING geoCodeDistance &amp;lt;=  &quot;</span>.<span style="color: #0000ff;">$radius</span>.<span style="color: #ff0000;">&quot;&quot;</span>;</pre></td></tr></table></div>

<p>At the end of the query, I could limit the number of results that were looked at in the select. It now takes under 1 second to run the whole thing. Much, much better result.</p>
<p>So with a table such as</p>

<div class="wp_codebox"><table width="100%" ><tr><td colspan="2" class="msgheader"><div class="right"><a href="###" class="smalltxt" onclick="copycode($('3code13'));">[Copy to clipboard]</a><a class="smalltxt" href="###" onclick="toggle_collapse('313');">[<span id="313_symbol">-</span>]</a></div><div class="left"><span id="l3code13"><a href="#" onclick="javascript:showCodeTxt('3code13'); return false;">View Code</a>MYSQL</span></div></td></tr><tr id="313"><td class="code" id="3code13"><pre class="mysql"><span style="color: #993333; font-weight: bold;">CREATE TABLE</span> `MyTable` <span style="color: #66cc66;">&#40;</span>
    `geoLongitude` <span style="color: #aa9933; font-weight: bold;">FLOAT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">9</span>,<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>,
    `geoLatitude` <span style="color: #aa9933; font-weight: bold;">FLOAT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">9</span>,<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> TYPE=MyISAM ;</pre></td></tr></table></div>

<p>I end up with a query similar to this</p>

<div class="wp_codebox"><table width="100%" ><tr><td colspan="2" class="msgheader"><div class="right"><a href="###" class="smalltxt" onclick="copycode($('3code14'));">[Copy to clipboard]</a><a class="smalltxt" href="###" onclick="toggle_collapse('314');">[<span id="314_symbol">-</span>]</a></div><div class="left"><span id="l3code14"><a href="#" onclick="javascript:showCodeTxt('3code14'); return false;">View Code</a>MYSQL</span></div></td></tr><tr id="314"><td class="code" id="3code14"><pre class="mysql"><span style="color: #993333; font-weight: bold;">SELECT</span> *,
<span style="color: #cc66cc;">3956</span> * <span style="color: #cc66cc;">2</span> * <span style="color: #993333; font-weight: bold;">atan2</span><span style="color: #66cc66;">&#40;</span>
            <span style="color: #993333; font-weight: bold;">sqrt</span><span style="color: #66cc66;">&#40;</span>
                <span style="color: #993333; font-weight: bold;">pow</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>
                    <span style="color: #993333; font-weight: bold;">sin</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span> * <span style="color: #66cc66;">&#40;</span>$lat1 - $lat2<span style="color: #66cc66;">&#41;</span>/<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#41;</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> +
                <span style="color: #993333; font-weight: bold;">cos</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span>  * $lat2<span style="color: #66cc66;">&#41;</span> *
                <span style="color: #993333; font-weight: bold;">cos</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span>  * $lat1<span style="color: #66cc66;">&#41;</span> *
                <span style="color: #993333; font-weight: bold;">pow</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>
                    <span style="color: #993333; font-weight: bold;">sin</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span> * <span style="color: #66cc66;">&#40;</span>$long1 - $long2<span style="color: #66cc66;">&#41;</span> / <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#41;</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#41;</span> ,
            <span style="color: #993333; font-weight: bold;">sqrt</span><span style="color: #66cc66;">&#40;</span>
                <span style="color: #cc66cc;">1</span>  - <span style="color: #66cc66;">&#40;</span>
                    <span style="color: #993333; font-weight: bold;">pow</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>
                        <span style="color: #993333; font-weight: bold;">sin</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span>  * <span style="color: #66cc66;">&#40;</span>$lat1 - $lat2<span style="color: #66cc66;">&#41;</span> / <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
                    <span style="color: #66cc66;">&#41;</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> +
                    <span style="color: #993333; font-weight: bold;">cos</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span>  * $lat2<span style="color: #66cc66;">&#41;</span> *
                    <span style="color: #993333; font-weight: bold;">cos</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span>  * $lat1<span style="color: #66cc66;">&#41;</span> *
                    <span style="color: #993333; font-weight: bold;">pow</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>
                        <span style="color: #993333; font-weight: bold;">sin</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0174</span>  * <span style="color: #66cc66;">&#40;</span>$long1 - $long2<span style="color: #66cc66;">&#41;</span>/<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
                    <span style="color: #66cc66;">&#41;</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> AS geoCodeDistance
<span style="color: #993333; font-weight: bold;">FROM</span> MyTable
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span>geoLatitude <span style="color: #993333; font-weight: bold;">BETWEEN</span> <span style="color: #ff0000;">'$min_lat'</span> <span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #ff0000;">'$max_lat'</span> 
    <span style="color: #993333; font-weight: bold;">AND</span> geoLongitude <span style="color: #993333; font-weight: bold;">BETWEEN</span> <span style="color: #ff0000;">'$min_lon'</span> <span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #ff0000;">'$max_lon'</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">HAVING</span> geoCodeDistance &lt;= <span style="color: #cc66cc;">50</span>
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> geoCodeDistance</pre></td></tr></table></div>

<p>As far as finding the latitude and longitude, there are a plethora of free resources out there to help you with that. Yahoo has released their API for geocoding as well as mapping. Hopefully Google won&#8217;t be far behind with an API for geocoding. Although the Yahoo one is excellent, and allows for quite a large number of queries to their server. You can find more info at http://developer.yahoo.net/maps/rest/V1/geocode.html</p>
<p>I wish I had all the links to the pages where I found some of this information, but I don&#8217;t. I Hope this helps someone out there.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eatatsporks.com/2008/03/20/calculating-distances-with-mysql-and-zip-codes/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
