问题:
I have a project in which I have to show nearby hotels within 10 kms of the current position of the user. I am successfully getting the result by the below given sql query....
可以将文章内容翻译成中文,广告屏蔽插件会导致该功能失效:
问题:
I have a project in which I have to show nearby hotels within 10 kms of the current position of the user. I am successfully getting the result by the below given sql query. Further if the user selects north/south/west/east direction then I have to show user only nearby hotels available in that particular direction. What can I do to know in which direction the nearby hotels are in?
$nearby = DB::select( DB::raw("SELECT * ,((((acos(sin((?*pi()/180)) * sin((`latitude*pi()/180))+cos((?*pi()/180)) * cos((latitude*pi()/180)) * cos(((?- longitude)*pi()/180))))*180/pi())*60*1.1515*1.609344)) as distance FROM hotel_data HAVING distance<= 10 ORDER BY distance") )->setBindings([$request->latitude,$request->latitude,$request->longitude]);`
回答1:
From dougv.com You want to use this function to get an accurate bearing that also keeps in mind the curvature of the earth.
function getRhumbLineBearing($lat1, $lon1, $lat2, $lon2) {
//difference in longitudinal coordinates
$dLon = deg2rad($lon2) - deg2rad($lon1);
//difference in the phi of latitudinal coordinates
$dPhi = log(tan(deg2rad($lat2) / 2 + pi() / 4) / tan(deg2rad($lat1) / 2 + pi() / 4));
//we need to recalculate $dLon if it is greater than pi
if(abs($dLon) > pi()) {
if($dLon > 0) {
$dLon = (2 * pi() - $dLon) * -1;
}
else {
$dLon = 2 * pi() + $dLon;
}
}
//return the angle, normalized
return (rad2deg(atan2($dLon, $dPhi)) + 360) % 360;
}
Then to turn it into a compass direction you can use this function, also on that same site:
function getCompassDirection($bearing) {
$tmp = round($bearing / 22.5);
switch($tmp) {
case 1:
$direction = "NNE";
break;
case 2:
$direction = "NE";
break;
case 3:
$direction = "ENE";
break;
case 4:
$direction = "E";
break;
case 5:
$direction = "ESE";
break;
case 6:
$direction = "SE";
break;
case 7:
$direction = "SSE";
break;
case 8:
$direction = "S";
break;
case 9:
$direction = "SSW";
break;
case 10:
$direction = "SW";
break;
case 11:
$direction = "WSW";
break;
case 12:
$direction = "W";
break;
case 13:
$direction = "WNW";
break;
case 14:
$direction = "NW";
break;
case 15:
$direction = "NNW";
break;
default:
$direction = "N";
}
return $direction;
}
Then when you've appointed a compass rose direction to all coordinates you can filter them easely.
$results = DB::select( DB::raw("SELECT * ....."));
/** get the direction as a compass direction **/
$user_direction = $request->direction;// N
/** group general directions togeter from a simple north, south, east west **/
$filter= [
'N' => ['N', 'NNE', 'NE', 'NW', 'NNW'],
'E' => ['E','ESE','ENE','NE', 'SE'],
'W' => ['S','SSW','SW','SE','SSE'],
'S' => ['W','WSW','WNW','SW', 'NW'],
];
/** get any direction that appears in this array ['N', 'NNE', 'NE', 'NW', 'NNW'] **/
$filter_direction = $filter[$user_direction];
/** The filtered locations are here in the $filtered variable **/
$filtered = $results->filter(function($item, $key) use ($filter_direction ) {
$bearing = getRhumbLineBearing($request->latitude,
$request->longitude,
$result->latitude,
$result->longitude);
$compass = getCompassDirection($bearing);
return in_array($compass);
})->all();