<?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>Pixels from the Edge &#187; Geocoding</title>
	<atom:link href="http://pixelsfromtheedge.com/tag/geocoding/feed/" rel="self" type="application/rss+xml" />
	<link>http://pixelsfromtheedge.com</link>
	<description>Creative // Technology // Digital // Interactive // Mobile // Advertising</description>
	<lastBuildDate>Tue, 24 Jan 2012 18:18:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Geocoding Part 2 &#8211; Import your own Zip Code Spatial Database</title>
		<link>http://pixelsfromtheedge.com/2009/04/geo-coding-part-2-import-your-own-zip/</link>
		<comments>http://pixelsfromtheedge.com/2009/04/geo-coding-part-2-import-your-own-zip/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 10:48:00 +0000</pubDate>
		<dc:creator>Richie</dc:creator>
				<category><![CDATA[Geocoding]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.detroitdigitalrevolution.com/clients/me/blog/?p=23</guid>
		<description><![CDATA[In my <a href="http://richie-p.com/blog/2009/04/geocoding-and-distancing-and-latitude/">post yesterday</a> I talked about calculating geographical distances.  During my research I’d come across this <a href="http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL" target="_new">http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL</a>.  It was a super interesting read and helped me get a much better understanding of what it is I am even doing.]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.pixelsfromtheedge.com/2009/04/geocoding-and-distancing-and-latitude/">post yesterday</a> I talked about calculating geographical distances.  During my research I’d come across this <a href="http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL" target="_new">http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL</a>.  It was a super interesting read and helped me get a much better understanding of what it is I am even doing.  Also it really helps you gets super optimized with the whole subject, but as the locations table I am working with has less than 3000 entries my queries were already coming back super fast in less than 1000th of a second, so it really didn’t need to be optimized more.  I didn’t really have time to mess around and investigate it all, and seeing as I still didn’t really fully understand the whole subject too well it was something I’d come back to at another time if need be.  However it did point me to <a href="http://www.geonames.org/" target="_new">geonames.org</a> that has free databases with geographical data to download.  This is incredibly useful and whenever I’ve needed this data in the past I’ve had my client buy it (the disk is like $30 so no biggie, but it seems this might even be the free source they are pulling it all from).  They have individual country zip codes files available to <a href="http://download.geonames.org/export/zip/" target="_new">export</a>, the US file contains exactly what I needed – all the US zip codes with their respective latitude and longitude coordinates.  This site has a whole range of useful features on the subject – they appear to even have their own <a href="http://www.geonames.org/export/web-services.html" target="_new">webservice</a>, and a list client libraries in various languages, which pointed me towards a <a href="http://www.solmetra.com/en/disp.php/en_products/en_scripts/en_maps" target="_new">PHP library</a> that appears to be doing a lot of what I need (but also way more than what I need), but by this point I already have everything I needed and had already spent too much time on the subject (not really but I didn’t have as much time allocated to the subject as it really needs – geo-location is a subject unto itself but only a small piece of the website I am building).</p>
<p>So geonames.org offers a free webservice, but I was already using the google API which is also free so I didn’t see any benefit to switch.  But they did offer for free all the zip codes with the geographical coordinates so I decided to import this into my database and cut out the 3rd party google Geocode API.  It would mean quicker response time and not having to worry about if the service is unavailable or returning errors, I was actually planning to cache the results locally anyway.  And perhaps the zip code table might have to be updated on an annual basis to maintain its accuracy (like do zip codes really change that much anyways?), but the web service API is subject to change and it would be less maintainable to monitor it and react to the changes.</p>
<p>So I downloaded the tab delimited US Zip Codes file and imported it into a table in my database called “tbl_Zipcodes_Spatial”, with the following fields (from the included readme):</p>
<p class="reverse">
Country_code      : iso country code, 2 characters<br />
Postal_code       : varchar(10)<br />
Place_name        : varchar(180)<br />
Admin_name1       : 1. order subdivision (state) varchar(100)<br />
Admin_code1       : 1. order subdivision (state) varchar(20)<br />
Admin_name2       : 2. order subdivision (county/province) varchar(100)<br />
Admin_code2       : 2. order subdivision (county/province) varchar(20)<br />
Admin_name3       : 3. order subdivision (community) varchar(100)<br />
Latitude          : estimated latitude (wgs84) float(10,6)<br />
Longitude         : estimated longitude (wgs84) float(10,6)<br />
Accuracy          : accuracy of lat/lng from 1=estimated to 6=centroid
</p>
<p>I created an index on the postal_code field and updated my GeoPos:: prepareGetByDistanceSQL() method to get the zip code from the new table.  I actually created a new method so that the class still has the ability to pass in the coordinates to the old method when necessary:</p>
<pre class="brush: php;">
&lt;?php
function prepareGetByDistanceWithZipcodeSQL($zip_code,$distance,$table_name,$fields,$units="miles"){
  $cUnit=self::convertMeasuringUnit($units);
  $sql="
    SELECT ".$table_name.".".implode($fields,",".$table_name.".").",
      tbl_zipcodes_spatial.Latitude AS Zipcode_Latitude,
      tbl_zipcodes_spatial.Longitude AS Zipcode_Longitude,
      ACOS(SIN( PI()* tbl_zipcodes_spatial.Latitude /180 )*SIN( PI()*".$table_name.".Latitude/180 ))+(COS(PI()* tbl_zipcodes_spatial.Latitude /180)*COS( PI()*".$table_name.".Latitude/180) *COS(PI()*".$table_name.".Longitude/180-PI()* tbl_zipcodes_spatial.Longitude /180))* ".$cUnit." AS Distance
    FROM tbl_zipcodes_spatial,".$table_name."
    WHERE
      tbl_zipcodes_spatial.postal_code='".$zip_code."'
      AND ".$cUnit." * ACOS( (SIN(PI()* tbl_zipcodes_spatial.Latitude /180)*SIN(PI() * ".$table_name.".Latitude/180)) +
        (COS(PI()* tbl_zipcodes_spatial.Latitude /180)*COS(PI()*".$table_name.".Latitude/180)*COS(PI() * ".$table_name.".Longitude/180-PI()* tbl_zipcodes_spatial.Longitude /180))
        ) &lt;= ".$distance."
    ORDER BY ".$cUnit." * ACOS(
      (SIN(PI()* tbl_zipcodes_spatial.Latitude /180)*SIN(PI()*".$table_name.".Latitude/180)) +
        (COS(PI()* tbl_zipcodes_spatial.Latitude /180)*COS(PI()*".$table_name.".Latitude/180)*COS(PI() * ".$table_name.".Longitude/180-PI()* tbl_zipcodes_spatial.Longitude /180))
      )
  ";
  return $sql;
}
?&gt;
</pre>
<p>And it worked!  Barely increased the query execution time, and I’ve completely cut out the google API middle man so the overall execution time of the page is greatly increased.  Now I feel like I’ve put together something pretty damn robust (the final touch would be to create a stored procedure in the database, and this is something I&#8217;ll come back to at a future point).  Behold the final script:</p>
<pre class="brush: php;">
&lt;?php
/*
Geographical Location functions
ALL CREDIT GOES TO http://blog.peoplesdns.com/
Thanks to these two blog posts:

http://blog.peoplesdns.com/archives/24

http://blog.peoplesdns.com/archives/34

This API:

http://code.google.com/apis/maps/documentation/geocoding/

And the zipcode database found here:

http://www.geonames.org/

*/

class GeoLoc{

	const GOOGLE_API_KEY="yourgooglekeygoeshere";
	const GOOGLE_API_URL="http://maps.google.com/maps/geo";

	/*
	queries the google api for zip code
	returns long and lat coordinates
	*/
	public function getZipcodeCoordinates($zip_code){
		$result=file_get_contents(self::GOOGLE_API_URL."?q=".$zip_code."&#038;sensor=false&#038;gl=us&#038;output=xml&#038;key=".self::GOOGLE_API_KEY);
		$data=simplexml_load_string($result);
		$coord=$data-&gt;Response-&gt;Placemark-&gt;Point-&gt;coordinates;
		$coord=explode(",",$coord);
		$r-&gt;longitude=$coord[0];
		$r-&gt;latitude=$coord[1];
		return $r;
	}

	/*
	returns the radius
	*/
	public function rad($v){
		return ($v*M_PI/180);
	}

	/*
	degree,minute,seconds to decimal degrees
	note: this could also be done in a single line using ternary
	*/
	public function dms2deg($D,$M,$S,$dir){
		if(strpos(' WsSs', $dir)&gt;0){
			return(-1 * ($D + ($M + $S/60)/60));
		}else{
			return($D + ($M + $S/60)/60);
		}
	}

	/*
	degree,minute,seconds
	*/
	public function dms($rad) {
		$d = abs($rad * 180 / M_PI);
		$d += 1/7200; // add ½ second for rounding
		$deg = floor($d);
		$min = floor(($d-$deg)*60);
		$sec = floor(($d-$deg-$min/60)*3600);
		// add leading zeros if required
		if ($deg&lt; 100) $deg = '0' + $deg;
		if ($deg&lt; 10) $deg = '0' + $deg;
		if ($min&lt; 10) $min = '0' + $min;
		if ($sec&lt; 10) $sec = '0' + $sec;
		return $deg + '\u00B0' + $min + '\u2032' + $sec + '\u2033';
	}

	/*
	gets the measuring unit
	*/
	public function convertMeasuringUnit($units){
		switch ($units){
			case "miles": $r = 3963.191; break;;
			case "nmiles": $r = 3441.596; break;;
			case "kilo": $r = 6378.137; break;;
		}
		return $r;
	}

	/*
	distance between two points using sherical law of cosines
	cos c = cos a cos b + sin a sin b cos C
	*/
	public function distance($lat1, $lon1, $lat2, $lon2, $units = 'miles'){
		$lat1 = self::rad($lat1); $lon1 = self::rad($lon1);
		$lat2 = self::rad($lat2); $lon2 = self::rad($lon2);
		$r=self::convertMeasuringUnit($units);

		return acos(sin($lat1)*sin($lat2) + cos($lat1)*cos($lat2)*cos($lon2-$lon1)) * $r;
	}

	/*
	initial bearing from point 1 to point 2
	*/
	public function bearing($lat1,$lon1, $lat2, $lon2) {
		$y = sin($lon2-$lon1) * cos($lat2);
		$x = cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($lon2-$lon1);
		return atan2($y, $x);
	}

	/*
	find out what the midpoint between two points is
	*/
	public function midpoint($lat1, $lon1, $lat2, $lon2) {
		$lat1 = self::rad($lat1); $lon1 = self::rad($lon1);
		$lat2 = self::rad($lat2); $lon2 = self::rad($lon2);
		$dLon = $lon2 - $lon1;
		$Bx = (cos($lat2) * cos($dLon));
		$By = (cos($lat2) * sin($dLon));
		$lat3 = atan2( sin($lat1) + sin($lat2), sqrt( (cos($lat1)+$Bx) * (cos($lat1)+$Bx) + ($By * $By)) );
		$lon3 = $lon1 + atan2($By, cos($lat1) + $Bx);

		if (!$lat3 || !$lon3) return false;
		return array($lat3 * 180 / M_PI, $lon3 * 180 / M_PI);
	}

	/*
	returns sql to query a table for rows by distance (miles,nmiles,kilo)
	table must contain fields named latitude and longitude
	*/
	public function prepareGetByDistanceSQL($long,$lat,$distance,$table_name,$fields,$units="miles"){
		$cUnit=self::convertMeasuringUnit($units);
		$sql="
			SELECT ".implode($fields,",").",
				acos(SIN( PI()* ".$lat." /180 )*SIN( PI()*latitude/180 ))+(cos(PI()* ".$lat." /180)*COS( PI()*latitude/180) *COS(PI()*longitude/180-PI()* ".$long." /180))* ".$cUnit." AS distance
			FROM ".$table_name."
			WHERE active=1
				AND ".$cUnit." * ACOS( (SIN(PI()* ".$lat." /180)*SIN(PI() * latitude/180)) +
				(COS(PI()* ".$lat." /180)*cos(PI()*latitude/180)*COS(PI() * longitude/180-PI()* ".$long." /180))
				) &lt;= ".$distance."
			ORDER BY ".$cUnit." * ACOS(
				(SIN(PI()* ".$lat." /180)*SIN(PI()*latitude/180)) +
				(COS(PI()* ".$lat." /180)*cos(PI()*latitude/180)*COS(PI() * longitude/180-PI()* ".$long." /180))
			)
		";
		return $sql;
	}

	/*
	returns sql to query a table for rows by distance (miles,nmiles,kilo)
	table must contain fields named latitude and longitude
	requires you have the tbl_zipcodes_spatial table http://download.geonames.org/export/zip/
	*/
	public function prepareGetByDistanceWithZipcodeSQL($zip_code,$distance,$table_name,$fields,$units="miles"){
		$cUnit=self::convertMeasuringUnit($units);
		$sql="
			SELECT ".$table_name.".".implode($fields,",".$table_name.".").",
				tbl_zipcodes_spatial.Latitude AS Zipcode_Latitude,
				tbl_zipcodes_spatial.Longitude AS Zipcode_Longitude,
				ACOS(SIN( PI()* tbl_zipcodes_spatial.Latitude /180 )*SIN( PI()*".$table_name.".Latitude/180 ))+(COS(PI()* tbl_zipcodes_spatial.Latitude /180)*COS( PI()*".$table_name.".Latitude/180) *COS(PI()*".$table_name.".Longitude/180-PI()* tbl_zipcodes_spatial.Longitude /180))* ".$cUnit." AS Distance
			FROM tbl_zipcodes_spatial,".$table_name."
			WHERE
				tbl_zipcodes_spatial.postal_code='".$zip_code."'
				AND ".$cUnit." * ACOS( (SIN(PI()* tbl_zipcodes_spatial.Latitude /180)*SIN(PI() * ".$table_name.".Latitude/180)) +
				(COS(PI()* tbl_zipcodes_spatial.Latitude /180)*COS(PI()*".$table_name.".Latitude/180)*COS(PI() * ".$table_name.".Longitude/180-PI()* tbl_zipcodes_spatial.Longitude /180))
				) &lt;= ".$distance."
			ORDER BY ".$cUnit." * ACOS(
				(SIN(PI()* tbl_zipcodes_spatial.Latitude /180)*SIN(PI()*".$table_name.".Latitude/180)) +
				(COS(PI()* tbl_zipcodes_spatial.Latitude /180)*COS(PI()*".$table_name.".Latitude/180)*COS(PI() * ".$table_name.".Longitude/180-PI()* tbl_zipcodes_spatial.Longitude /180))
			)
		";
		return $sql;
	}
}
?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://pixelsfromtheedge.com/2009/04/geo-coding-part-2-import-your-own-zip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Geocoding and Distancing and Latitude and Longitude and umm Stuff</title>
		<link>http://pixelsfromtheedge.com/2009/04/geo-coding-and-distancing-and-latitude/</link>
		<comments>http://pixelsfromtheedge.com/2009/04/geo-coding-and-distancing-and-latitude/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 19:01:00 +0000</pubDate>
		<dc:creator>Richie</dc:creator>
				<category><![CDATA[Geocoding]]></category>
		<category><![CDATA[Google API]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.detroitdigitalrevolution.com/clients/me/blog/?p=22</guid>
		<description><![CDATA[Okay this is complicated stuff.  Working on putting together a new site for a client, I have a table of locations with longitude and latitude fields for every row (I actually had a tab delimited file, but I sucked it into the db).  For the functionality on the web front end a user needs to search by distance, e.g. find all locations within 10 miles of a zip code.  Um okay, so how the hell do I do this?
Well first of I need to get the latitude and longitude for a zip code.  For that I very quickly found&#8230;]]></description>
			<content:encoded><![CDATA[<p>Okay this is complicated stuff.  Working on putting together a new site for a client, I have a table of locations with longitude and latitude fields for every row (I actually had a tab delimited file, but I sucked it into the db).  For the functionality on the web front end a user needs to search by distance, e.g. find all locations within 10 miles of a zip code.  Um okay, so how the hell do I do this?</p>
<p>Well first of I need to get the latitude and longitude for a zip code.  For that I very quickly found the <a href="http://code.google.com/apis/maps/documentation/geocoding/" target=_new>Google Geocode API</a> and threw together the following:</p>
<pre class="brush: php;">
&lt;?php
$result=file_get_contents("http://maps.google.com/maps/geo?48220&amp;sensor=false&amp;gl=us&amp;output=xml&amp;key=yourgooglekey);
$data=simplexml_load_string($result);
$coord=$data->Response->Placemark->Point->coordinates;
$coord=explode(",",$coord);
$longitude=$coord[1];
$latitude=$coord[0];
?&gt;
</pre>
<p>It took like 10 minutes, and that included signing up for the API key and reading the documentation, shit this geocoding stuff is easy, I should be done by lunch…</p>
<p>Yeah okay so I have two sets of coordinates, great but errr now what?  So I find the <a href="http://en.wikipedia.org/wiki/Haversine_formula" target=_new>Haversine</a> formula, but alas not really being any good at math I don’t understand it, but it doesn’t really matter because I know how to convert it to do the math in PHP.  Cool so I got the Haversine function in PHP now but how do I query it against the data in my database?  I’d read some stuff about geographical libraries for databases and it seemed super complicated, like the <a href="http://dev.mysql.com/tech-resources/articles/4.1/gis-with-mysql.html" target=_new>GIS and Spatial extensions for MySQL</a>.  So I figure I’ll use a two step approach and do a simple query to pull out a square block of locations and then apply the math.  i.e. if I am searching for within a 10 mile radius of a location I’ll do a search on -10 miles and +10 miles, but as the distance is circular I would be getting results in the four corners of that square block that are greater than the 10 miles, and I would then apply the Haversine function and be left only with those results that where truly within the 10 mile radius.</p>
<p>By sheer luck I found <a href="http://blog.peoplesdns.com/archives/24" target=_new>this post</a>.  It was insane, I didn’t realize you could apply the Haversine formula directly in SQL, I ran that query in my db and bam it worked instantly (well actually there’s a typo syntax error ‘< =’ -blank space between the less than and equals character - in the where clause).  Obviously I had to switch out a couple of things so it played nice with my database, but it was instant success, I tried out some different latitude and longitude coordinates and it worked every time:</p>
<p class="reverse">
SELECT Name,Address,Address2,City,State,Zip,Phone,URL,Latitude,Longitude,<br />    acos(SIN( PI()* 42.4584036 /180 )*SIN( PI()*latitude/180 ))+(cos(PI()* 42.4584036 /180)*COS( PI()*latitude/180) *COS(PI()*longitude/180-PI()* -83.1380955 /180))* 3963.191 AS distance<br />   FROM tbl_retailers<br />   WHERE active=1<br />    AND 3963.191 * ACOS( (SIN(PI()* 42.4584036 /180)*SIN(PI() * latitude/180)) +<br />    (COS(PI()* 42.4584036 /180)*cos(PI()*latitude/180)*COS(PI() * longitude/180-PI()* -83.1380955 /180))<br />    ) <= 100    ORDER BY 3963.191 * ACOS(     (SIN(PI()* 42.4584036 /180)*SIN(PI()*latitude/180)) +     (COS(PI()* 42.4584036 /180)*cos(PI()*latitude/180)*COS(PI() * longitude/180-PI()* -83.1380955 /180))    )
</p>
<p>Wow so this was it, everything I needed.  Oh wait, except that I needed to display the distance in miles.  But the guy who blogged this was some kind of superman and he talks about this on <a href="http://blog.peoplesdns.com/archives/34" target=_new>another post</a>.  Really I only needed the rad() and distance() functions (because distance is dependent on rad), but I decided that they’ll probably all come in useful one day, so I took them wrapped them in a class called GeoLoc.  The class doesn’t need to be instantiated into an object so all methods can be called as follows</p>
<pre class="brush: php;">
&lt;?php
GeoLoc::distance($lat1, $lon1, $lat2, $lon2, $units);
?&gt;
</pre>
<p>Then decided I could abstract in the SQL statement by passing in all necessary variables, and I added it to the class:</p>
<pre class="brush: php;">
&lt;?php
GeoLoc::prepareGetByDistanceSQL($lat,$long,$distance_in_miles,$table_name,$fields);
?&gt;
</pre>
<p>I then understood that the measuring unit the query is using is the same as in the distance() method so I abstracted it out into its own method.  At which point I noticed that the numbers didn’t match exactly – for miles the SQL had 3963.191 whereas the distance function only had 3963.1.  I decided that the SQL query had the more granular number and when I reviewed its original blog post it was across the board for all three measuring units so I updated all the numbers.  I then figured I might as well abstract in the Google Geocode zip code lookup.</p>
<p>This is what I was left with (functions I&#8217;m not using removed); I give all credit to joeldg:</p>
<pre class="brush: php;">
&lt;?php
/*
Geographical Location functions
ALL CREDIT GOES TO http://blog.peoplesdns.com/
Thanks to these two blog posts:

http://blog.peoplesdns.com/archives/24

http://blog.peoplesdns.com/archives/34

This API:

http://code.google.com/apis/maps/documentation/geocoding/

*/
class GeoLoc{

const GOOGLE_API_KEY="yourkeygoeshere";
const GOOGLE_API_URL="http://maps.google.com/maps/geo";

/*
queries the google api for zip code
returns long and lat coordinates
*/
function getZipcodeCoordinates($zip_code){
  $result=file_get_contents(self::GOOGLE_API_URL."?q=".$zip_code."&#038;sensor=false&#038;gl=us&#038;output=xml&#038;key=".self::GOOGLE_API_KEY);
  $data=simplexml_load_string($result);
  $coord=$data-&gt;Response-&gt;Placemark-&gt;Point-&gt;coordinates;
  $coord=explode(",",$coord);
  $r-&gt;longitude=$coord[0];
  $r-&gt;latitude=$coord[1];
  return $r;
}
function rad($v){
  return ($v*M_PI/180);
}
/*
distance between two points using sherical law of cosines
cos c = cos a cos b + sin a sin b cos C
*/
function distance($lat1, $lon1, $lat2, $lon2, $units = 'miles'){
  $lat1 = self::rad($lat1); $lon1 = self::rad($lon1);
  $lat2 = self::rad($lat2); $lon2 = self::rad($lon2);
  $r=self::convertMeasuringUnit($units);
  return acos(sin($lat1)*sin($lat2) + cos($lat1)*cos($lat2)*cos($lon2-$lon1)) * $r;
}
/*
gets the measuring unit
*/
function convertMeasuringUnit($units){
  switch ($units){
    case "miles": $r = 3963.191; break;;
    case "nmiles": $r = 3441.596; break;;
    case "kilo": $r = 6378.137; break;;
  }
  return $r;
}
/*
returns sql to query a table for rows by distance (miles,nmiles,kilo)
table must contain fields named latitude and longitude
*/
function prepareGetByDistanceSQL($long,$lat,$distance,$table_name,$fields,$units="miles"){
  $cUnit=self::convertMeasuringUnit($units);
  $sql="
    SELECT ".implode($fields,",").",
    acos(SIN( PI()* ".$lat." /180 )*SIN( PI()*latitude/180 ))+(cos(PI()* ".$lat." /180)*COS( PI()*latitude/180) *COS(PI()*longitude/180-PI()* ".$long." /180))* ".$cUnit." AS distance
    FROM ".$table_name."
    WHERE active=1
      AND ".$cUnit." * ACOS( (SIN(PI()* ".$lat." /180)*SIN(PI() * latitude/180)) +
    (COS(PI()* ".$lat." /180)*cos(PI()*latitude/180)*COS(PI() * longitude/180-PI()* ".$long." /180))
    ) &lt;= ".$distance."
    ORDER BY ".$cUnit." * ACOS(
    (SIN(PI()* ".$lat." /180)*SIN(PI()*latitude/180)) +
    (COS(PI()* ".$lat." /180)*Cos(PI()*latitude/180)*COS(PI() * longitude/180-PI()* ".$long." /180))
   )
  ";
  return $sql;
}
}
?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://pixelsfromtheedge.com/2009/04/geo-coding-and-distancing-and-latitude/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

