Last active
August 29, 2015 14:06
-
-
Save BastinRobin/2c0b886187679913cab9 to your computer and use it in GitHub Desktop.
Geo Location Search Using Radius
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 getLocation() { | |
$lat = 37; | |
$lon = -122; | |
$radius = 40; | |
$result = Zipcode::select( | |
DB::raw("*, | |
( 3959 * acos( cos( radians(?) ) * | |
cos( radians( lat ) ) | |
* cos( radians( lng ) - radians(?) | |
) + sin( radians(?) ) * | |
sin( radians( lat ) ) ) | |
) AS distance")) | |
->having("distance", "<", $radius) | |
->orderBy("distance") | |
->setBindings([$lat, $lon, $lat]) | |
->get(); | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment