Created
May 27, 2011 12:54
-
-
Save garak/995175 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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