Skip to content

Instantly share code, notes, and snippets.

@swilla
Created March 21, 2013 15:49
Show Gist options
  • Save swilla/5214093 to your computer and use it in GitHub Desktop.
Save swilla/5214093 to your computer and use it in GitHub Desktop.
Radius search
$params = array();
$params['range'] = 10; //Miles
$params['zip'] = 19711; //Zip Code
//Get Lat,Lon from specified zip
$query = $this->db->query("SELECT lat,lon FROM default_zip_code WHERE zip_code=".$params['zip']);
if ($query->num_rows() > 0)
{
$row = $query->row();
$latitude = $row->lat;
$longitude = $row->lon;
if(!empty($params['range'])){
$range = $params['range'];
}else{
$range = 10;
}
$this->db->select(' z.lat, z.lon, ( 3956 *2 * ATAN2( SQRT( POW( SIN( ( RADIANS( z.lat ) - RADIANS( '.$latitude.' ) ) /2 ) , 2 ) + COS( RADIANS( '.$latitude.' ) ) * COS( RADIANS( z.lat ) ) * POW( SIN( (
RADIANS( z.lon ) - RADIANS( '.$longitude.' ) ) /2 ) , 2 ) ) , SQRT( 1 - ( POW( SIN( ( RADIANS( z.lat ) - RADIANS( '.$longitude.' ) ) /2 ) , 2 ) + COS( RADIANS( '.$longitude.' ) ) * COS( RADIANS( z.lat ) ) * POW( SIN( (
RADIANS( z.lon ) - RADIANS( '.$longitude.' ) ) /2 ) , 2 ) ) ) ) ) AS distance',FALSE);
$this->db->from('default_zip_codes z');
$this->db->where('venues.zip = z.zip_code');
$this->db->having('distance <='.$range,FALSE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment