Skip to content

Instantly share code, notes, and snippets.

@garak
Created May 27, 2011 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garak/995175 to your computer and use it in GitHub Desktop.
Save garak/995175 to your computer and use it in GitHub Desktop.
<?php
// lib/model/ItemQuery.php
class ItemQuery extends BaseItemQuery
{
/**
* @param array $values
* @param integer $limit
* @return PropelCollection
*/
public function search($values, $limit = 10)
{
$lat = floatval($values['latitude']);
$lng = floatval($values['longitude']);
// replace 6371 with 3959 if you want miles instead of kilometres
return $this->create()
->withColumn('(6371 * acos(cos(radians(' . $lat . ')) * cos(radians(latitude)) * cos(radians(longitude) - radians(' . $lng . ')) + sin(radians(' . $lat . ')) * sin(radians(latitude))))', 'distance')
->orderBy('distance')
->where('distance <= ?', $values['radius'])
->limit($limit)
->find();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment