Skip to content

Instantly share code, notes, and snippets.

@mheadd
Created June 4, 2011 18:59
Show Gist options
  • Select an option

  • Save mheadd/1008202 to your computer and use it in GitHub Desktop.

Select an option

Save mheadd/1008202 to your computer and use it in GitHub Desktop.
function getSql($max,$type,$lat,$lon) {
GLOBAL $round, $limit, $crime_types;
$sql = "SELECT BLOCKSITEADDRESS, OFFENSE, REPORTDATETIME, ROUND(((ACOS( SIN($lat * PI()/180 ) * SIN(LATITUDE * PI()/180 ) + COS($lat * PI()/180 ) * COS(LATITUDE * PI()/180 ) * COS(($lon - LONGITUDE) * PI()/180))*180/PI())*60*1.1515),$round) AS DISTANCE FROM crime_incidents_2008 ";
if($type > 0) {
$sql .= 'WHERE OFFENSE = \''.$crime_types[$type].'\' ';
}
$sql .= "HAVING distance <= $max ORDER BY DISTANCE ASC LIMIT 0 , $limit";
return $sql;
}
<?php
// Calculate the distance between 2 points.
function calculateDistance($lat1, $lon1, $lat2, $lon2) {
return 3958*3.1415926*sqrt(($lat2-$lat1)*($lat2-$lat1) + cos($lat2/57.29578)*cos($lat1/57.29578)*($lon2-$lon1)*($lon2-$lon1))/180;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment