gpt4 book ai didi

php - 想出了空间用户搜索,但我如何按距离排序? (php laravel mysql)

转载 作者:行者123 更新时间:2023-11-29 06:00:54 25 4
gpt4 key购买 nike

经过一夜的折腾,我成功地实现了按距离搜索用户!我多年来一直想实现的目标。但是我还有另一个问题。我无法按距离对它们进行排序。

我有一个用户表。每个用户都有一个“国家”、“州”、“邮政编码”和“郊区”。

我还有一个包含以下字段的 places 表。

'国家'、'州'、'邮政编码'和'郊区'、经度、纬度

我设法破解了一个查询,该查询按距离顺序返回所有地点。但现在我想按距离顺序返回该郊区、邮政编码、州和国家/地区的所有用户。

这是我目前的代码,我不确定如何修改它。

//spatial queries

static function cmp($user1, $user2)
{
global $places;

foreach($places as $place)
{
if(($place->country == $user1->country) && ($place->suburb == $user1->suburb) && ($place->state == $user1->state))
{
return true;
}
if(($place->country == $user2->country) && ($place->suburb == $user2->suburb) && ($place->state == $user2->state))
{
return false;
}
}
return false;
}

public function search()
{
if (Auth::guest())
{
return Redirect::guest('/');
}
else
{
if ( Session::token() !== request( '_token' ) ) {
return Response::json( array(
'searcherror' => 'Invalid Authorization Token'
) );
}





//identify the right query (make sure later: if you arent what they are looking for, they dont show up. If you are blocked, they dont show up)


if(request('country') && request('lfmin') && request('lfmax'))
{

//main requirements exist
if(!request('state') && !request('suburb') && !request('postcode') && request('distance'))
{





//spatial queries

//$circle_radius = 3959; //miles
$circle_radius = 6371; //kms
$max_distance = request('distance');
$lat = Auth::user()->getLoc()->latitude;
$lng = Auth::user()->getLoc()->longitude;

//get all nearby places
$places = DB::select(
'SELECT * FROM
(SELECT id, country, suburb, state, latitude, longitude, (' . $circle_radius . ' * acos(cos(radians(' . $lat . ')) * cos(radians(latitude)) *
cos(radians(longitude) - radians(' . $lng . ')) +
sin(radians(' . $lat . ')) * sin(radians(latitude))))
AS distance
FROM places) AS distances
WHERE distance < ' . $max_distance . '
ORDER BY distance;
');


/*

$result = User::contains('country', $places->country)
->contains('state', $places->state)
->contains('postcode', $places->postcode)
->contains('suburb', $places->suburb)
->get();
*/

$countryarray = [];
$suburbarray = [];
$statearray = [];

foreach($places as $place)
{
//get country array
//get state array
//get suburb array
array_push($countryarray, $place->country);
array_push($suburbarray, $place->suburb);
array_push($statearray, $place->state);
}

$places = (object)$places;


$users = User::whereIn('suburb', $suburbarray)
->whereIn('country', $countryarray)
->whereIn('state', $statearray)->get();

//put this somewhere useful later remember usort(array,'cmp')

$users = $users->toArray(); // Convert user collection to array

//static


usort($users, array("App\Http\Controllers\SearchController", "cmp"));





return Response::json( array(
'searcherror' => $users
) );
}

编辑:试过了,固定类路径得到了这个

为 foreach() 提供的参数无效

你的排序函数有问题

注意:用户位置以及经度和纬度可以通过用户对象中的方法访问

$用户->getLoc()所以基本上我们可以像这样访问它$user->getLoc()->经度

最佳答案

您现在可以使用自定义比较器对用户数组进行排序,该比较器将使用排序后的位置数组在两个用户之间进行比较。

代码示例-

class SomeController
{
function someFunction()
{
$places = ...; // The places are already calculated by you
$users = ...; // Users are also fetched in the code

$users = $users->toArray(); // Convert user collection to array

usort($users, function($user1, $user2) use($places)
{
foreach($places->toArray() as $place)
{
if(($place->country == $user1->country) && ($place->suburb == $user1->suburb) && ($place->state == $user1->state))
{
return true;
}
if(($place->country == $user2->country) && ($place->suburb == $user2->suburb) && ($place->state == $user2->state))
{
return false;
}
}
return false;
});

// The array $users is now sorted based on user places
}
}

您还可以使用 hashmap 类型的结构优化位置搜索,这将加快代码的复杂性。

关于php - 想出了空间用户搜索,但我如何按距离排序? (php laravel mysql),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45068205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com