php – Laravel中两点之间的Haversine距离计算

我正在开发一个laravel应用程序,我需要找到所有在用户坐标的某个半径范围内的产品.产品与用户有一对多的关系,因此用户可以拥有多种产品.

我发现,半正式公式可以计算出两点之间的距离,但我似乎无法使它起作用.

我的控制器中有以下查询

$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->纬度’,但似乎没有任何效果.

这是我的模特:

产品型号

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');
    }
}
最佳答案
这是我的实施.我提前选择别名查询,这样我就可以利用分页.此外,您需要显式选择要从查询中检索的列.将它们添加到 – > select().例如users.latitude,users.longitude,products.name或者它们可能是什么.

我创建了一个看起来像这样的范围:

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]);
}

您可以将此范围应用于具有纬度和经度的任何模型.

将$location->纬度替换为您要搜索的纬度,并将$location->经度替换为您要搜索的经度.

使用您希望在$location附近找到的模型替换model.latitude和model.longitude,具体取决于$radius中定义的距离.

我知道你有一个有效的haversine公式,但是如果你需要Paginate,你不能使用你提供的代码.

希望这会有所帮助.

相关文章

目录MySQL卸载环境查看是否已安装MySQL卸载mysql服务查看是否...
目录数据类型数据类型分类数值类型以TINYINT认识整型族有符号...
目录表的约束空属性非空约束(NOT NULL Constraint)默认值定...
目录函数时间日期函数:字符串函数数学函数其他函数 函数 时间...
目录使用C语言连接库的安装C APImysql_initmysql_real_conne...
目录用户用户管理查询所有用户查看当前用户查看当前连接数创...