digitalcloud / laravel-postgis

v1.0.6 2019-07-11 10:11 UTC

README

本包用于计算或检查点与数据库中其他点之间的距离。

安装

需要PHP >=7.1.3和Laravel ^5.6。

本包使用Laravel postgis扩展来处理Laravel中的PostgreSQL数据库点,因此需要更多详细信息或如何在PHP中启用PostGIS扩展,请参阅前面的链接。

要获取Laravel PostGIS的最新版本,只需使用Composer安装项目。

composer require digitalcloud/laravel-postgis

用法

1. 首先在您的模型中使用Postgis特质

<?php

namespace App;

use Digitalcloud\Postgis\Postgis;
use Illuminate\Database\Eloquent\Model;

class UserLocation extends Model
{
    use Postgis;
}

2. 默认情况下,包假设点列的名称为location,如果您想更改它,则在模型中覆盖location变量

protected $location = "my_column";

3. 此外,默认情况下,包假设距离的单位为meter,如果您想更改它,则在模型中覆盖unit变量

 protected $unit = "km"; //units avialble [mile, km, meter]

函数

1. withDistance

获取点与其他点之间的距离

UserLocation::withDistance(new Point($atitude,$longitude))
            ->with("user")
            ->whereIn("user_id", $users)
            ->get();

2. whereDistance

检查点与数据库中其他点之间的距离

       UserLocation::whereDistance(new Point($atitude,$longitude), ">", 50)
            ->with("user")
            ->whereIn("user_id", $users)
            ->get();