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

Rob*_*e22 7 php mysql sql haversine laravel

我正在开发一个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();
Run Code Online (Sandbox Code Playgroud)

我将半径设置为20000用于测试目的,看起来所有产品的距离都是5687,..问题似乎是产品的纬度和经度存储在User表中,但我不确定我如何在我的查询中访问这些.我已经尝试过user.latitude'user-> latitude',但似乎没有任何效果.

这是我的模特:

产品型号

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;
}
Run Code Online (Sandbox Code Playgroud)

用户模型

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');
    }
}
Run Code Online (Sandbox Code Playgroud)

Ohg*_*why 16

这是我的实施.我提前选择别名查询,这样我就可以利用Pagination.此外,您需要显式选择要从查询中检索的列.将它们添加到->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]);
}
Run Code Online (Sandbox Code Playgroud)

您可以将此范围应用于带有latitude和的任何模型longitude.

更换$location->latitude你的latitude,你想对搜索,并替换$location->longitude与您希望搜索对经度.

根据在中定义的距离,替换model.latitudemodel.longitude您希望找到的模型.$location$radius

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

希望这会有所帮助.


Ser*_*iaz 1

如果您愿意使用外部包,我建议使用无限有用的 PHPGeo 库。我在一个依赖这些精确计算的项目中使用了它,并且效果很好。它可以让您从头开始自己编写计算,并且经过测试可以正常工作。

https://github.com/mjaschen/phpgeo

以下是 Harvesine 的文档:https://phpgeo.marcusjaschen.de/#_distance_ Between_two_coordinates_haversine_formula