yieldstudio/nova-google-autocomplete
Composer 安装命令:
composer require yieldstudio/nova-google-autocomplete
包简介
A Laravel Nova Google autocomplete field.
README 文档
README
This field allows you to work with Google Places API to autocomplete on user input and get the full real address with all the metadata (like latitude and longitude).
Fork from emilianotisato/nova-google-autocomplete-field to maintain package.
Installation
You can install the package in to a Laravel app that uses Nova via composer:
composer require yieldstudio/nova-google-autocomplete
Now publish config and localization files:
php artisan vendor:publish --provider="YieldStudio\NovaGoogleAutocomplete\FieldServiceProvider"
Create an app and enable Places API and create credentials to get your API key https://console.developers.google.com
Add the below to your .env file
NOVA_GOOGLE_AUTOCOMPLETE_API_KEY=############################
Usage
Add the use declaration to your resource and use the fields:
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']),
You can access other parameter like latitude, longitude, street_number, route, locality, administrative_area_level_1, country, postal_code, along with everything available in the - every field present in the PlaceResult object
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(),
By default, the formatted address will be stored on the property provided in the GoogleAutocomplete field. If you don't want to store it, you can use the dontStore method:
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'),
Combine Values
If you want to concatenate certain elements of the geocoded object that is returned by Google, using {{ and }}, wrap the key like you would above; like so:
use YieldStudio\NovaGoogleAutocomplete\AddressMetadata; use YieldStudio\NovaGoogleAutocomplete\GoogleAutocomplete; GoogleAutocomplete::make('Address')->withValues(['latitude', 'longitude']), AddressMetadata::make('coordinates')->fromValue('{{latitude}}, {{longitude}}'),
So the value that would be rendered within the coordinates input would be something like:
39.3315476, -94.9363912
Define Short/Long Value
If you would like to use the long_name version of the geocoded object (Kansas versus KS), you can define the GoogleAutocomplete field values with dot notation followed with the name version you want to use; like so:
use YieldStudio\NovaGoogleAutocomplete\GoogleAutocomplete; GoogleAutocomplete::make('Address') ->withValues([ 'route.short_name', 'administrative_area_level_1.long_name', ]),
Which would return:
route: W 143rd St
administrative_area_level_1: Kansas
You can change the type of places that are returned by the autocomplete using the placeType() method. You can use any of the values listed at 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');
Capturing all values as JSON
If you want to capture all requested values as a JSON object, you can use the fromValuesAsJson() helper instead of using 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(),
Localization
If you want this package in your language, just create a json lang file in your resources/lang/vendor/nova-google-autocomplete folder.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail contact@yieldstudio.fr instead of using the issue tracker.
Credits
- Emiliano Tisato - Original developer
- James Hemery - Fork maintainer
- Simon Depelchin - Contributor
License
The MIT License (MIT). Please see License File for more information.
yieldstudio/nova-google-autocomplete 适用场景与选型建议
yieldstudio/nova-google-autocomplete 是一款 基于 Vue 开发的 Composer 扩展包,目前已累计 236.75k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2022 年 06 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「address」 「autocomplete」 「google」 「nova」 「laravel」 「yieldstudio」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yieldstudio/nova-google-autocomplete 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yieldstudio/nova-google-autocomplete 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yieldstudio/nova-google-autocomplete 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The Cassandra php driver library IDE stubs which enables autocompletion in modern IDEs.
Maps in minutes. Powered by the Google Maps API.
Redirects TYPO3 visitors automatic or with a suggestlink to another language and/or root page.
Module adding custom shipping attribute for what3words address
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
Dadata API client (https://dadata.ru)
统计信息
- 总下载量: 236.75k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-01