我有一个项目,其中我必须显示距离用户当前位置10公里以内的附近酒店。我通过以下给定的SQL查询成功获取结果。此外,如果用户选择了北/南/西/东方向,那么我只能向用户显示该特定方向附近的可用酒店。我该怎么办才能知道附近的酒店在哪个方向?
$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]);`
答案 0 :(得分:2)
从dougv.com起,您想使用此功能来获得精确的方位角,同时还要牢记地球的曲率。
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;
}
然后将其转换为指南针方向,您也可以在同一站点上使用此功能:
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;
}
然后,当您为所有坐标指定指南针上升方向时,就可以轻松对其进行过滤。
$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();