<?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>PHP Web developer, Robert Kern &#187; Google</title>
	<atom:link href="http://www.robertkern.com/tags/google/feed" rel="self" type="application/rss+xml" />
	<link>http://www.robertkern.com</link>
	<description>Solid PHP Web Development with SEO and web standards in mind.</description>
	<lastBuildDate>Thu, 18 Mar 2010 20:05:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<cloud domain='www.robertkern.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Finding objects close to a location with MySQL</title>
		<link>http://www.robertkern.com/web-development/finding-objects-close-to-a-location-with-mysql.html</link>
		<comments>http://www.robertkern.com/web-development/finding-objects-close-to-a-location-with-mysql.html#comments</comments>
		<pubDate>Wed, 07 May 2008 01:38:10 +0000</pubDate>
		<dc:creator>Robert Kern</dc:creator>
				<category><![CDATA[How to]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.robertkern.com/?p=65</guid>
		<description><![CDATA[For a project I&#8217;m currently working on, I needed to be able to find people that a within a certain distance from a particular point. I have a large database of people that each have a Longitude and Latitude of their location. One of the searches in the app I am building needs to find [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>For a project I&#8217;m currently working on, I needed to be able to find people that a within a certain distance from a particular point.</p>
<p>I have a large database of people that each have a Longitude and Latitude of their location.  One of the searches in the app I am building needs to find people that are close to the location of the Job.  So if the Job is in Kumeu (NZ), I want to see the people that are close (lets say 25km).</p>
<p>This is made easy with the <a href="http://en.wikipedia.org/wiki/Haversine_formula" target="_blank">Haversine</a> formula.  Google has a <a href="http://code.google.com/support/bin/answer.py?answer=87134&amp;topic=11364" target="_blank">good article</a> that gives a MySQL query that will produce a list of rows from the database where the locations are within a certain number of kilometres/miles.</p>
<p>The MySQL query looks like this:<br />
<code><br />
SELECT *,<br />
(6371 *<br />
acos(cos(radians(174.556107)) * cos(radians(latitude))<br />
* cos(radians(longitude) - radians(-36.775700))<br />
+ sin(radians(174.556107)) * sin(radians(latitude))))<br />
AS distance<br />
FROM Locations<br />
HAVING distance &lt; 25<br />
ORDER BY distance<br />
</code><br />
The location in the example above (-36.775700, 174.556107) is of Kumeu (NZ).  The table currently has 4,500 records and the query takes about 0.0063 seconds (actually it is usually 0.0001 seconds).</p>
<p>Note that the above query calculates kilometres.  If you are wanting miles, change the figure of 6371 to 3959.</p>
<p>The HAVING distance &lt; 25 means that we are only getting the records who are within 25km of the original location.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.robertkern.com/web-development/finding-objects-close-to-a-location-with-mysql.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
