This class helps with the creation of criteria/scopes to filter models by geographical distance from a given latitude/longitude.
The recommended approach, is to add one or more scope methods to your models, so that the exact behavior is built into the model and easy to reuse - here is an example/template scope method, using all of the key features:
public function near($lat, $long, $dist = 10)
{
$helper = new GDistanceHelper('t.geo_lat', 't.geo_long');
$c = $this->getDbCriteria();
// select all columns from the root table:
$c->mergeWith(array('select' => 't.*'));
// optimize the query by first filtering results by bounds:
$c->mergeWith($helper->getBoundsCriteria($lat, $long, $dist));
// filter the results further, by exact max. distance:
$c->mergeWith($helper->getMaxRadiusCriteria($lat, $long, $dist));
// compute the radius (as a column) and bind to $distance:
$c->mergeWith($helper->getRadiusCriteria($lat, $long, $dist));
// order results by distance:
$c->mergeWith(array('order' => $helper->alias));
return $this;
}
It's a single class/file, just drop into your components directory and get on with it.
Download or fork ΒΆ
No guarantees, no support, use at your own risk.
Tested with MySQL as the backing DBMS only.
Shut up and love it!
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.