brandonjbegle / nova4-google-autocomplete-field
这个Nova包提供了一个字段,允许您使用Google Place API自动完成地址,并获取完整的元数据(经纬度等)。
Requires
- php: ^8.0
- laravel/nova: ^4.0
README
此字段允许您使用Google Places API与用户输入自动完成,并获取包含所有元数据(如纬度和经度)的完整真实地址。
安装
您可以通过composer将此包安装到使用Nova的Laravel应用程序中
composer require brandonjbegle/nova4-google-autocomplete-field
现在发布配置和本地化文件
php artisan vendor:publish --provider="BrandonJBegle\GoogleAutocomplete\FieldServiceProvider"
创建一个应用并启用Places API,然后创建凭证以获取您的API密钥 https://console.developers.google.com
将以下内容添加到您的.env
文件中
ADDRESS_AUTOCOMPLETE_API_KEY=############################
使用方法
将使用声明添加到您的资源中并使用字段
use BrandonJBegle\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 BrandonJBegle\GoogleAutocomplete\AddressMetadata; use BrandonJBegle\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 BrandonJBegle\GoogleAutocomplete\AddressMetadata; use BrandonJBegle\GoogleAutocomplete\GoogleAutocomplete; GoogleAutocomplete::make('Address')->withValues(['latitude', 'longitude']), AddressMetadata::make('coordinates')->fromValue('{{latitude}}, {{longitude}}'),
因此,在坐标输入中渲染的值可能如下所示
39.3315476, -94.9363912
定义短/长值
如果您想使用地理编码对象的long_name
版本(堪萨斯州与KS),您可以使用点符号定义GoogleAutocomplete
字段值,并附加您想要使用的名称版本;如下所示
use BrandonJBegle\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 BrandonJBegle\GoogleAutocomplete\AddressMetadata; use BrandonJBegle\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');
启用当前位置按钮
GoogleAutocomplete::make('Address')->enableCurrentLocation();
本地化
如果您希望此包支持您的语言,只需在您的resources/lang/vendor/google-autocomplete
文件夹中创建一个json lang文件。示例
resources/lang/vendor/google-autocomplete/es.json
感谢Emiliano Tisato的原始Nova Google AutoComplete Field,这是此Nova 4包的来源