emilianotisato/nova-google-autocomplete-field

这个Nova包是一个字段,允许您使用Google Place API自动完成地址并获取完整的元数据(经纬度等)。

0.8.0 2021-03-26 17:56 UTC

This package is auto-updated.

Last update: 2024-08-27 01:24:30 UTC


README

此字段允许您使用Google Places API处理用户输入并获取完整的真实地址以及所有元数据(如纬度和经度)。

安装

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

composer require emilianotisato/nova-google-autocomplete-field

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

php artisan vendor:publish --provider="EmilianoTisato\GoogleAutocomplete\FieldServiceProvider"

创建一个应用,启用Places API并创建凭证以获取您的API密钥 https://console.developers.google.com

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

ADDRESS_AUTOCOMPLETE_API_KEY=############################

用法

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

use EmilianoTisato\GoogleAutocomplete\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 EmilianoTisato\GoogleAutocomplete\AddressMetadata;
use EmilianoTisato\GoogleAutocomplete\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(),

组合值

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

use EmilianoTisato\GoogleAutocomplete\AddressMetadata;
use EmilianoTisato\GoogleAutocomplete\GoogleAutocomplete;

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

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

因此,在坐标输入中渲染的值可能是这样的

39.3315476, -94.9363912

定义短/长值

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

use EmilianoTisato\GoogleAutocomplete\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 EmilianoTisato\GoogleAutocomplete\AddressMetadata;
use EmilianoTisato\GoogleAutocomplete\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');

本地化

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

resources/lang/vendor/google-autocomplete/es.json