gabelbart/nova-geolocation-field

一个Laravel Nova字段。

0.2.3 2023-05-05 08:27 UTC

This package is auto-updated.

Last update: 2024-09-05 11:31:46 UTC


README

此包为Laravel Nova提供了一种地图上选择地理坐标的输入控件。此外,还可以进行地址地理编码。

字段不会在模型上写入值。相反,它将与您需要提供的纬度和经度字段同步。

要求

  • php: ^8.0
  • laravel/nova: ^4.0

安装/开始使用

通过Composer安装包

composer require gabelbart/nova-geolocation-field

发布静态资源

php  artisan vendor:publish --tag=laravel-assets --ansi

设置资源类

  1. 添加\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation字段
  2. 添加纬度和经度字段
<?php

namespace App\Nova;

use Laravel\Nova\Fields\Hidden;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Http\Requests\NovaRequest;

use Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation;

class User extends Resource
{
    public static $model = \App\Models\User::class;
    public static $title = 'name';
    public static $search = ['id'];

    public function fields(NovaRequest $request): array
    {
        return [
            ID::make()->sortable(),

            Hidden::make('latitude'),
            Hidden::make('longitude'),
            Geolocation::make('Map position'),
        ];
    }

    // snip...
}

注意:您还可以使用用户可以看到/输入所选纬度/经度的数字字段。同步在两个方向上都有效。

使用方法

选项

defaultLatitude(float $latitude)

设置地图中心的默认纬度。

\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::make('Map position')
  ->defaultLatitude(52.520008); // Berlin

defaultLatitude(float $latitude)

设置地图中心的默认纬度。

\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::make('Map position')
  ->defaultLongitude(13.404954); // Berlin

defaultZoom(int $zoom)

设置地图的默认缩放级别

\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::make('Map position')
  ->defaultZoom(10);

latitudeField(string $fieldName)

纬度字段的属性名称。

\Laravel\Nova\Fields\Number::make('Latitude', 'my_latitude');
\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::make('Map position')
  ->latitudeField('my_latitude');

longitudeField(string $fieldName)

纬度字段的属性名称。

\Laravel\Nova\Fields\Number::make('Longitude', 'my_longitude');
\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::make('Map position')
  ->longitudeField('my_longitude');

streetField(string $fieldName)

街道字段的属性名称。

\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::make('Map position')
  ->streetField('street');

streetNumberField(string $fieldName)

街道编号/门牌号字段的属性名称。

\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::make('Map position')
  ->streetNumberField('street_number');

postalCodeField(string $fieldName)

邮政编码字段的属性名称。

\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::make('Map position')
  ->postalCodeField('postal_code');

cityField(string $fieldName)

城市字段的属性名称。

\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::make('Map position')
  ->cityField('city');

countryField(string $fieldName)

国家字段的属性名称。

\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::make('Map position')
  ->countryField('country');

regionField(string $fieldName)

地区/州字段的属性名称。

\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::make('Map position')
  ->regionField('region');

selectionMode(string $fieldName)

更改选择模式。可以在地图上通过移动鼠标(\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::SELECTION_MODE_MOVE)或双击地图(\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::SELECTION_MODE_DBCLICK)来选择位置。

SELECTION_MODE_MOVE是默认设置。

\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::make('Map position')
  ->selectionMode(\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::SELECTION_MODE_DBCLICK);

enableGeocoding(bool $flag = true)

启用地理编码功能。要使地理编码功能正常工作,您需要安装和设置spatie/geocoder:^3。您还需要提供至少streetFieldcityFieldcountryField选项。

\String::make('Street');
\String::make('City');
\String::make('Country');
\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::make('Map position')
  ->cityField('street')
  ->cityField('city')
  ->cityField('country')
  ->enableGeocoding();

writeBackGeocodedAddress(bool $flag = true)

此选项需要配置地址字段并启用地理编码。当选择地理编码地址时,地址组件的值将被写回到输入字段。

\String::make('Street');
\String::make('Street Number');
\String::make('City');
\String::make('Zip');
\String::make('Country');
\String::make('Region');
\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::make('Map position')
  ->writeBackGeocodedAddress()
  ->cityField('street')
  ->cityField('street_number')
  ->cityField('city')
  ->postalCodeField('zip')
  ->cityField('country')
  ->regionField('region')
  ->enableGeocoding();

更改地理编码结果的缓存

默认情况下,地理编码请求将被缓存7天。每次缓存命中时,此周期将刷新。

禁用缓存

在您的服务提供程序中调用\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::disableGeocodingCache静态方法以禁用缓存。这将导致Google地理编码API的负载增加,因此可能会增加成本。

更改缓存持续时间

要更改地理编码缓存的TTL,请调用您的服务提供程序中的\Gabelbart\Laravel\Nova\Fields\Geolocation\Geolocation::geocodingCacheTtl静态方法。它接受分钟或有效的\DateInterval实例。