在我的脚本中,我将返回所有位于用户当前位置50KM范围内的项目,如此
//user's current location
$events = $query->fetchAll(PDO::FETCH_ASSOC);
$aryEvents = [];
foreach ($events as $event) {
$distance = distanceGeoPoints($event['event_latitude'], $event['event_longitude'], $params['user_latitude'], $params['user_longitude']);
//for events greater than or equal to distance than that of coordinates
if($distance >= 50) {
continue;
}
DistanceGeopoints()函数
function distanceGeoPoints ($lat1, $lon1, $lat2, $lon2) {
$earthRadius = 6371;
$dLat = deg2rad($lat2-$lat1);
$dLon = deg2rad($lon2-$lon1);
$a = sin($dLat/2) * sin($dLat/2) +
cos(deg2rad($lat1)) * cos(deg2rad($lat2)) *
sin($dLon/2) * sin($dLon/2);
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
$dist = $earthRadius * $c;
return $dist;
}
我想了解两件事 1.为什么大于“> =”的对象返回此范围内的对象(这就是我想要的,我只是不明白为什么)
当我尝试在半径30米范围内找到特定事件时,它会按照距离的顺序放置事件,这不是我想要的。我只想要在这个较小半径范围内的特定事件,否则它们应该按照原始顺序, 这是我尝试解决以下问题
if ($distance >= 0.03 && $event['event_user_id'] != 0) {
$inserted = array($event);
array_splice($aryEvents,0,0,$inserted);
} else {
array_push($aryEvents, $event);
}
任何帮助都会有很大帮助!