jeorgy/laravel-postgis

v1.12.0 2021-05-22 16:08 UTC

This package is auto-updated.

Last update: 2024-09-29 06:02:59 UTC


README

此包实现了从数据库中计算或检查点与其他点之间的距离,以及检索区域内的点,无论是任意多边形还是圆形,这是对DigitalCloud Laravel-Posgis的分支。

实现了在geoJSON多边形中搜索点的功能:whereCovers

安装

PHP >=7.1.3 和 Laravel ^7.x 需要。

该包使用Laravel postgis 扩展来处理laravel中的postgres数据库点,因此如果需要更多信息或如何启用php中的postgis扩展,请参阅前面的链接。

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

composer require jeorgy/laravel-postgis

使用方法

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

<?php

namespace App;

use Jeorgy\LaravelPostgis\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($latitude,$longitude))
            ->with("user")
            ->whereIn("user_id", $users)
            ->get();

2. whereDistance 和 orWhereDistance

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

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

3. whereCovers 和 orWhereCovers

获取geoJSON多边形内的点

       UserLocation::whereCovers($geoJson)
            ->with("user")
            ->get();