Laravel中两点之间的Haversine距离计算 [英] Haversine distance calculation between two points in Laravel
问题描述
我正在研究laravel应用程序,其中我需要找到用户坐标一定半径内的所有产品.产品与用户具有一对多的关系,因此用户可以拥有多个产品.
I'm working on a laravel appliciation in which I need to find all the products that are within a certain radius of the user's coordinates. Products have a one to many relationship with users, so users can have multiple products.
我发现Haversine公式可以计算两点之间的距离,但是我似乎无法使它起作用.
I've found that the haversine formula can calculate the distance between two points, but I can't seem to make it work.
我的控制器中有以下查询:
I've got the following query in my controller:
$latitude = 51.0258761;
$longitude = 4.4775362;
$radius = 20000;
$products = Product::with('user')
->selectRaw("*,
( 6371 * acos( cos( radians(" . $latitude . ") ) *
cos( radians(user.latitude) ) *
cos( radians(user.longitude) - radians(" . $longitude . ") ) +
sin( radians(" . $latitude . ") ) *
sin( radians(user.latitude) ) ) )
AS distance")
->having("distance", "<", $radius)
->orderBy("distance")
->get();
出于测试目的,我已将半径设置为20000,看来所有产品的距离均为5687. 问题似乎是产品的纬度和经度存储在User表中,但是我不确定如何在查询中访问这些产品.我尝试了 user.latitude 和'user-> latitude',但似乎没有任何效果.
I've set the radius to 20000 for testing purposes and it appears all products have a distance of 5687,.. The problem seems to be that the latitude and longitude of the products are stored in the User table, but I'm not sure how I can access those in my query. I've tried user.latitude and 'user->latitude' but nothing seems to work.
这是我的模特:
产品型号
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable =
[
'soort',
'hoeveelheid',
'hoeveelheidSoort',
'prijsPerStuk',
'extra',
'foto',
'bio'
];
public function User()
{
return $this->belongsTo('App\User');
}
public $timestamps = true;
}
用户模型
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
protected $table = 'users';
protected $fillable =
[
'firstName',
'lastName',
'adres',
'profilepic',
'description',
'longitude',
'latitude',
'email',
'password'
];
protected $hidden = ['password', 'remember_token'];
public function product()
{
return $this->hasMany('App\Product');
}
}
推荐答案
这是我的实现.我选择提前为查询加上别名,这样我就可以利用Pagination
的优势.此外,您需要明确选择要从查询中检索的列.将它们添加到->select()
.例如users.latitude, users.longitude, products.name
或它们可能的任何形式.
This was my implementation of it. I've chosen to alias my query out ahead of time, this way I can take advantage of Pagination
. Furthermore, you need to explicitly select the columns that you wish to retrieve from the query. add them at the ->select()
. Such as users.latitude, users.longitude, products.name
, or whatever they may be.
我创建了一个看起来像这样的范围:
I have created a scope which looks something like this:
public function scopeIsWithinMaxDistance($query, $location, $radius = 25) {
$haversine = "(6371 * acos(cos(radians($location->latitude))
* cos(radians(model.latitude))
* cos(radians(model.longitude)
- radians($location->longitude))
+ sin(radians($location->latitude))
* sin(radians(model.latitude))))";
return $query
->select() //pick the columns you want here.
->selectRaw("{$haversine} AS distance")
->whereRaw("{$haversine} < ?", [$radius]);
}
您可以将此范围应用于具有latitude
和longitude
的任何模型.
You can apply this scope to any model with a latitude
andlongitude
.
将$location->latitude
替换为要搜索的latitude
,然后将$location->longitude
替换为希望搜索的经度.
Replace the $location->latitude
with your latitude
that you wish to search against, and replace the $location->longitude
with the longitude that you wish to search against.
根据$radius
中定义的距离,将model.latitude
和model.longitude
替换为希望在$location
周围找到的模型.
Replace the model.latitude
and model.longitude
with the Models you wish to find around the $location
based on the distance defined in the $radius
.
我知道您有一个有效的Haversine公式,但是如果需要分页,则无法使用您提供的代码.
I know you have a functioning Haversine formula, but if you need to Paginate you can't use the code you've supplied.
希望这会有所帮助.
这篇关于Laravel中两点之间的Haversine距离计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!