Skip to content

Instantly share code, notes, and snippets.

@1stevengrant
Forked from fhferreira/haversine.php
Created January 24, 2017 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1stevengrant/7593b32160cb630b6f0503527d41787a to your computer and use it in GitHub Desktop.
Save 1stevengrant/7593b32160cb630b6f0503527d41787a to your computer and use it in GitHub Desktop.
Haversine Method for model Laravel.
<?php
/*
* find the n closest locations
* @param Model $query eloquent model
* @param float $lat latitude of the point of interest
* @param float $lng longitude of the point of interest
* @param float $max_distance distance in miles or km
* @param string $units miles or kilometers
* @param Array $fiels to return
* @return array
*/
public static function haversine($query, $lat, $lng, $max_distance = 25, $units = 'miles', $fields = false )
{
if(empty($lat)){
$lat = 0;
}
if(empty($lng)){
$lng = 0;
}
/*
* Allow for changing of units of measurement
*/
switch ( $units ) {
case 'miles':
//radius of the great circle in miles
$gr_circle_radius = 3959;
break;
case 'kilometers':
//radius of the great circle in kilometers
$gr_circle_radius = 6371;
break;
}
/*
* Support the selection of certain fields
*/
if( ! $fields ) {
$fields = array( 'users.*', 'users_profile.*', 'users.username as user_name' );
}
/*
* Generate the select field for disctance
*/
$distance_select = sprintf(
"
ROUND(( %d * acos( cos( radians(%s) ) " .
" * cos( radians( lat ) ) " .
" * cos( radians( lng ) - radians(%s) ) " .
" + sin( radians(%s) ) * sin( radians( lat ) ) " .
" ) " .
")
, 2 ) " .
"AS distance
",
$gr_circle_radius,
$lat,
$lng,
$lat
);
$data = $query->select( DB::raw( implode( ',' , $fields ) . ',' . $distance_select ) )
->having( 'distance', '<=', $max_distance )
->orderBy( 'distance', 'ASC' )
->get();
//echo '<pre>';
//echo $query->toSQL();
//echo $distance_select;
//echo '</pre>';
//die();
//
//$queries = DB::getQueryLog();
//$last_query = end($queries);
//var_dump($last_query);
//die();
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment