Skip to content

Instantly share code, notes, and snippets.

@JoaoVagner
Created April 24, 2013 14:17
Show Gist options
  • Save JoaoVagner/5452452 to your computer and use it in GitHub Desktop.
Save JoaoVagner/5452452 to your computer and use it in GitHub Desktop.
geoNear MongoDB + Monga
public function getByLatLng($lat, $lng, $range = 5000 /* KM */, $earthRadius = 6378 /* Unit Earth Radius */) {
\Config::load('db');
$mongoConf = \Config::get('mongo');
$monga = \Monga::connection('mongodb://' . $mongoConf['default']['hostname'] . ':27017');
$database = $monga->database($mongoConf['default']['database']);
$nameCache = 'company_get_by_' . $lat . '_' . $lng;
try {
return \Fuel\Core\Cache::get($nameCache);
} catch (\CacheNotFoundException $e) {
$get = $database->command(array(
'geoNear' => $this->collection,
'near' => array((float) $lat, (float) $lng),
'nearSphere' => true,
'maxDistance' => $range / $earthRadius
)
);
\Fuel\Core\Cache::set($nameCache, $get);
return \Fuel\Core\Cache::get($nameCache);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment