Created
April 24, 2013 14:17
-
-
Save JoaoVagner/5452452 to your computer and use it in GitHub Desktop.
geoNear MongoDB + Monga
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
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