Return to Snippet

Revision: 43454
at March 24, 2011 05:39 by uberdragon


Initial Code
$lat = trim($_REQUEST['lat']);
$lng = trim($_REQUEST['lng']);

$lat = (!empty($lat)? $lat : 41.3423502);
$lng = (!empty($lng)? $lng : -73.0774616);

$HelpQuery = sprintf("SELECT *, ( 3959 * acos( cos( radians( %s ) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians( %s ) ) + sin( radians( %s ) ) * sin( radians( lat ) ) ) ) AS distance FROM databaseTable ORDER BY distance LIMIT 0,5",$lat,$lng,$lat);

$HelpResult = mysql_query($HelpQuery);
$result = array();
$result['info'] = "";

if($HelpResult)
{
	while($Helprow = mysql_fetch_assoc($HelpResult))
	{
			$Helprow['distance'] = round($Helprow['distance'],2);
			$result['info'][] = $Helprow; 
	}
}
else
{
	$result['info'] = "Error";
}

Initial URL

                                

Initial Description
Expects databaseTable to have both lat and lon columns with appropriate location coordinate data.

Initial Title
Get closest locations by latitude longitude SQL math statement

Initial Tags

                                

Initial Language
PHP