geographical-distance-helper

Build scopes/criteria using your models' latitude/longitude coordinates to filter by (and/or compute) distance from a point.
9 followers

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!

Be the first person to leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet