yieldstudio/nova-google-autocomplete

Laravel Nova Google 自动完成字段。

1.3.0 2023-08-16 10:25 UTC

This package is auto-updated.

Last update: 2024-09-16 12:52:24 UTC


README

Latest Version Total Downloads

此字段允许您使用 Google 地点 API 对用户输入进行自动完成,并获取包含所有元数据(如纬度和经度)的完整真实地址。

emilianotisato/nova-google-autocomplete-field 分支以维护包。

安装

您可以通过 composer 安装该包到使用 Nova 的 Laravel 应用程序中

composer require yieldstudio/nova-google-autocomplete

现在发布配置和本地化文件

php artisan vendor:publish --provider="YieldStudio\NovaGoogleAutocomplete\FieldServiceProvider"

创建一个应用,启用地点 API 并创建凭据以获取 API 密钥 https://console.developers.google.com

将以下内容添加到您的 .env 文件中

NOVA_GOOGLE_AUTOCOMPLETE_API_KEY=############################

用法

将使用声明添加到您的资源中并使用字段

use YieldStudio\NovaGoogleAutocomplete\GoogleAutocomplete;
// ....

GoogleAutocomplete::make('Address'),

//You can add a country or countries to autocomplete or leave empty for all.

// Specify a single country
GoogleAutocomplete::make('Address')
          ->countries('US'),

// Specify multiple countries [array]
GoogleAutocomplete::make('Address')
          ->countries(['US','AU']),

您可以访问其他参数,如 latitude, longitude, street_number, route, locality, administrative_area_level_1, country, postal_code,以及所有在 PlaceResult 对象 中可用的内容

use YieldStudio\NovaGoogleAutocomplete\AddressMetadata;
use YieldStudio\NovaGoogleAutocomplete\GoogleAutocomplete;

// Now this address field will search and store the address as a string, but also made available the values in the withValues array
GoogleAutocomplete::make('Address')->withValues(['latitude', 'longitude']),

// And you can store the values by autocomplete like this
AddressMetadata::make('lat')->fromValue('latitude'),
AddressMetadata::make('long')->fromValue('longitude'),

// You can disable the field so the user can't edit the metadata
AddressMetadata::make('long')->fromValue('longitude')->disabled(),

// Or you can make the field invisible in the form but collect the data anyways
AddressMetadata::make('long')->fromValue('longitude')->invisible(),

默认情况下,格式化地址将存储在 GoogleAutocomplete 字段提供的属性中。如果您不想存储它,可以使用 dontStore 方法。

use YieldStudio\NovaGoogleAutocomplete\AddressMetadata;
use YieldStudio\NovaGoogleAutocomplete\GoogleAutocomplete;

// Formatted address will not be stored
GoogleAutocomplete::make('Address')->withValues(['latitude', 'longitude'])->dontStore(),

// This field will be stored
AddressMetadata::make('lat')->fromValue('latitude'),
AddressMetadata::make('long')->fromValue('longitude'),

合并值

如果您想使用 Google 返回的地理编码对象的某些元素进行连接,使用 {{}} 将键包裹起来,就像上面一样。

use YieldStudio\NovaGoogleAutocomplete\AddressMetadata;
use YieldStudio\NovaGoogleAutocomplete\GoogleAutocomplete;

GoogleAutocomplete::make('Address')->withValues(['latitude', 'longitude']),

AddressMetadata::make('coordinates')->fromValue('{{latitude}}, {{longitude}}'),

因此,在坐标输入中渲染的值可能如下所示

39.3315476, -94.9363912

定义短/长值

如果您想使用地理编码对象的 long_name 版本(堪萨斯州与 KS),您可以使用点符号定义 GoogleAutocomplete 字段值,后面跟着您想要使用的名称版本;如下所示

use YieldStudio\NovaGoogleAutocomplete\GoogleAutocomplete;

GoogleAutocomplete::make('Address')
    ->withValues([
        'route.short_name',
        'administrative_area_level_1.long_name',
    ]),

这将返回

route: W 143rd St

administrative_area_level_1: Kansas

您可以使用 placeType() 方法更改自动完成返回的地点类型。您可以使用在 https://developers.google.com/places/supported_types#table3 中列出的任何值。

use YieldStudio\NovaGoogleAutocomplete\AddressMetadata;
use YieldStudio\NovaGoogleAutocomplete\GoogleAutocomplete;

// This autocomplete field will return results that match a business name instead of address.
// All the same address data is still stored.
GoogleAutocomplete::make('Address')->placeType('establishment');

捕获所有值作为 JSON

如果您想将所有请求的值作为 JSON 对象捕获,可以使用 fromValuesAsJson() 辅助程序而不是使用 fromValue()

// Autocomplete field
GoogleAutocomplete::make('Location')
    ->countries('BE')
    ->withValues(['latitude', 'longitude', 'street_number', 'route', 'locality', 'administrative_area_level_1', 'country', 'postal_code']),

// Field that will capture de response object
AddressMetadata::make('Address')
    ->fromValuesAsJson()
    ->invisible()
    ->onlyOnForms(),

// Display the response object in a Code field
Code::make('Address')->json()->onlyOnDetail(),

本地化

如果您想用您的语言使用此包,只需在 resources/lang/vendor/nova-google-autocomplete 文件夹中创建一个 json lang 文件。

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全

如果您发现了关于安全性的错误,请通过电子邮件发送到 contact@yieldstudio.fr,而不是使用问题跟踪器。

鸣谢

许可证

MIT许可(MIT)。请参阅许可文件获取更多信息。