bagusindrayana/laravel-coordinate

从Eloquent Laravel获取附近位置

0.0.3 2021-07-12 05:41 UTC

This package is auto-updated.

Last update: 2024-09-10 09:56:24 UTC


README

基于坐标使用Eloquent Laravel查找最近的数据

安装

composer require bagusindrayana/laravel-coordinate

  • 在模型中
#use trait
use Bagusindrayana\LaravelCoordinate\Traits\LaravelCoordinate;

class Toko extends Model
{
    use LaravelCoordinate;

    //optional
    public $_latitudeName = "latitude_column"; //default name is latitude
    public $_longitudeName = "longitude_column"; //default name is longitude

    //

}
  • 使用特性
    //get data at a distance of 500 meters (0.5KM)
    $tokos = Toko::nearby([
        -0.497493,//latitude
        117.156480//longitude
    ],0.5)->get();


    //using order, remember that the order is always placed at the end of the query
    //get data with a distance of 1 kilometer and sort it from farthest
    $tokos = Toko::nearby([
        -0.497493,//latitude
        117.156480//longitude
    ],1)->farthest()->get();
    
    //get data with a distance of 1 kilometer and sort it from closest to closest
    $tokos = Toko::nearby([
        -0.497493,//latitude
        117.156480//longitude
    ],1)->closest()->get();
    
    //add a custom column containing the distance value of each record
    $tokos = Toko::nearby([
        -0.497493,//latitude
        117.156480//longitude
    ],0.5) //0.5 Km
    ->selectDistance(['id','nama_toko'],'_distance') //this function will add a custom column/alias with the name "_distance" which contains the distance value of each record
    ->get();

公式

我还没有尝试它能处理多少数据以及计算速度有多快,所以这里提供了3种不同的公式您可以尝试

公式参数/参数(int)

  • 0 = 默认
  • 1 = 余弦定律
  • 2 = 水平距离公式

示例

$tokos = Toko::nearby([
        -0.497493,//latitude
        117.156480//longitude
    ],
    0.5,
    1//using Spherical Law of Cosines
)
->get();

范围

nearby(coordinate,radius/distance = 5,formula = 0)
closest(coordinate,formula = 0)
farthest(coordinate,formula = 0)
selectDistance(fieldName,aliasName,formula = 0)
insideBox(coorinate(2 coordinate))